19

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

10

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)

  • The first command didn't print trailing slashes for me, but the second one does. – Steven Dec 16 '10 at 17:45
15
find . \( -type d -printf "%p/\n" , -type f -print \)

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

  • I continue to be amazed at the flexibility of find. – Shawn J. Goff Dec 15 '10 at 4:12
  • 1
    Note that -printf is specific to GNU find. – Gilles 'SO- stop being evil' Dec 15 '10 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 '17 at 15:52
  • 1
    Based on this answer and Gilles', I came up with the following simplified version: find . -type d -printf '%p/\n' -or -print – Teresa e Junior Nov 11 '17 at 4:38
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.

  • @Gilles: Thanks for the "d" ;-) – ddeimeke Dec 15 '10 at 12:40
  • this increases the amount of syscalls – Paweł Polewicz Nov 11 '17 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.