I want to list the files in each directory the directory contains sub directories as well, in the directories i should ignore few directories it may be parent directory or sub directory. From the below script i able to list the files only in one directory its not looping around other directories. please let me know how can i resolve this issue.

Script which I have tried

find * -type d | while IFS= read d; do
    dirname=`basename $d`
        if [ ${dirname} != "Decommissioned" ]; then
          cd $dirname
          find * ! -name . -prune -type f | while read fname; do
             fname=`basename $fname`
             echo $fname

2 Answers 2


If you want to list all regular files but skipping those in directories named Decommissioned, you'd do:

find . -name Decommissioned -type d -prune -o -type f -print

If you want their basename only, with GNU find, you can replace the -print with -printf '%f\n'. Or POSIXly:

find . -name Decommissioned -type d -prune -o -type f -exec sh -c '
  for file do;
    printf "%s\n" "${file##*/}"
  done' sh {} +

Or if you can guarantee that none of the file names contain newline characters:

find . -name Decommissioned -type d -prune -o -type f -print |
  awk -F / '{print $NF}'

the below code will ignore the "Decommissioned" directory and list out the files in other directories.

find . -type d | while read DirName
   echo "${DirName}" | grep "Decommissioned" >/dev/null 2>&1
   if [ "$?" -ne "0" ]
        find ${DirName} -type f | awk -F/ '{print $NF}'

Answer based on the comment

bash-4.1$ find .

bash-4.1$ find . | grep -vE "a/|b2" | grep "\.txt"
  • Hey Kamaraj, thank you for your reply but i am getting error in using grep command grep: illegal option -- q Usage: grep -hblcnsviw pattern file . . . i have below os SunOS g2902usdsswf01v 5.10 Generic_150400-52 sun4v sparc sun4v
    – absar
    Nov 6, 2017 at 4:58
  • try the above one.
    – Kamaraj
    Nov 6, 2017 at 6:41
  • The files are repeating twice let me explain you the process.
    – absar
    Nov 6, 2017 at 9:40
  • I have three directories with subdirectories the parent directory are a, b, & c. a1&a2 are subdirectories of a, b1&b2 are subdirectories of b, c1&c2 are subdirectories of c. The files are arranged like this format a.txt(a), a1.txt(a1), a2.txt(a2), b.txt(b), b1.txt(b1), b2.txt(b2), c.txt(c), c1.txt(c1), c2.txt(c2). Now I am trying to list the files like i want to ignore the files in the entire directory a with all its a1&a2 sub directories and want to igonre the files only in the b2 directory not directory b or b1.
    – absar
    Nov 6, 2017 at 9:40
  • Hey Kamaraj, i tried your script like this find . -type d | while read DirName do echo "${DirName}" | grep "a" >/dev/null 2>&1 if [ "$?" -ne "0" ] then find ${DirName} -type f | awk -F/ '{print $NF}' fi done I found the files are listing twice, do we need to stop it somewhere to list the files only one time
    – absar
    Nov 8, 2017 at 4:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .