5

I'm looking for a shell command in order to get all the files in a complex directory structure. By complex directory structure I mean that there's a root folder with more than 150 subfolders and for each subfolder there are minimum 3 subfolders.

migrated from serverfault.com Mar 15 '13 at 16:45

This question came from our site for system and network administrators.

  • What do you want to do with the list of files? What you want to do with the list affects the best way to get it. – depquid Mar 15 '13 at 18:44
17

This is trivial:

find /directory -type f
8

As suggested by @michael-hampton find is the way to go. However a bit more explanation is needed. The option -type can have several values, check the man page (man find). Here is a quick definition:

If you are looking for regular files only:

find <path> -type f

If you are looking for anything not a directory:

find <path> ! -type d

If you are looking for regular files and symbolic links:

find <path> -type f -o -type l

(the previous command looks for regular OR link)

3

find works just as well. But if you are using zshell (zsh):

For only files:

ls -al path/**/*(.)

For only dirs:

ls -al path/**/*(/)

See man zshexpn for more eamples. Specifically the "glob qualifier" section.

1

You can also do this in bash4+ (be aware that this follows symlinks):

shopt -s globstar nullglob
for file in **/*; do
    [[ -f $file ]] && printf '%s\n' "$file"
done
  • The silly code coloring makes me want to put a comment # */ just to fix it. – kojiro Mar 15 '13 at 22:37
  • 1
    @kojiro - Sorry, forgot to force the syntax highlighter to bash mode. – Chris Down Mar 16 '13 at 23:53
  • 2
    Now I'm exceedingly happy I posted that dumb comment, because I didn't know you could force the highlighter to bash mode. – kojiro Mar 17 '13 at 0:34
  • 1
    @kojiro, in case you not found it yet, see Syntax highlighting for code in the Markdown help. – manatwork Mar 19 '13 at 7:27

Your Answer

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