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
find . \( -type d -printf "%p/\n" , ! -type d -print \)
This uses the printf
command to format directory names and standard print for the rest.
-
1
-
2
-
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.– jimpAug 16, 2017 at 15:52 -
3Based 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
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.– StevenDec 16, 2010 at 17:45
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.