24

How can I make the find command show a slash after directories? For example, I want dir to show up as dir/ instead of dir. I'm using find . -print

3 Answers 3

19
find . \( -type d -printf "%p/\n" , ! -type d -print \)

This uses the printf command to format directory names and standard print for the rest.

4
  • 1
    I continue to be amazed at the flexibility of find. Dec 15, 2010 at 4:12
  • 2
    Note that -printf is specific to GNU find. Dec 15, 2010 at 8:20
  • Nice answer. I like how find's positional arguments allow it to do advanced output like this. However, note that without the spaces around the comma, it will not work.
    – jimp
    Aug 16, 2017 at 15:52
  • 3
    Based on this answer and Gilles', I came up with the following simplified version: find . -type d -printf '%p/\n' -or -print Nov 11, 2017 at 4:38
13

Portably:

find . -type d -exec sh -c 'printf "%s/\n" "$0"' {} \; -or -print

If you're willing to list directories and files separately (you can merge the output by sorting):

{ find . -type d -print | sed 's!$!/!'; find . \! -type d; } | sort

With GNU find, see Shawn J. Goff's answer.

If you're willing to risk non-printable characters being mangled even when not outputting to a terminal, see ddeimeke's answer.

In zsh: print -rl -- **/*(DM) (D to include dot files, M to add a / after directories)

1
  • The first command didn't print trailing slashes for me, but the second one does.
    – Steven
    Dec 16, 2010 at 17:45
8

Maybe

find . -print0 | xargs -0 ls -Fd

is an option as well.

From a Solaris man page:

-F Marks directories with a trailing slash (/), doors with a trailing greater-than sign (>), executable files with a trailing asterisk (*), FIFOs with a trailing vertical bar (|), symbolic links with a trailing "at" sign (@), and AF_UNIX address family sockets with a trailingequals sign (=). Follows symlinks named as operands.

2
  • @Gilles: Thanks for the "d" ;-)
    – ddeimeke
    Dec 15, 2010 at 12:40
  • this increases the amount of syscalls Nov 11, 2017 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.