Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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

This is trivial:

find /directory -type f
share|improve this answer

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)

share|improve this answer

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.

share|improve this answer

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"
share|improve this answer
The silly code coloring makes me want to put a comment # */ just to fix it. – kojiro Mar 15 '13 at 22:37
@kojiro - Sorry, forgot to force the syntax highlighter to bash mode. – Chris Down Mar 16 '13 at 23:53
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
@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 posting your answer, you agree to the privacy policy and terms of service.