1

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

#!/bin/sh
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
          done
        else
           continue
        fi
done
1

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}'
0

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

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

Answer based on the comment

bash-4.1$ find .
.
./c
./c/c.txt
./c/c2
./c/c2/c2.txt
./c/c1
./c/c1/c1.txt
./a
./a/a.txt
./a/a2
./a/a2/a2.txt
./a/a1
./a/a1/a1.txt
./b
./b/b2
./b/b2/b2.txt
./b/b1
./b/b1/b1.txt
./b/b.txt

bash-4.1$ find . | grep -vE "a/|b2" | grep "\.txt"
./c/c.txt
./c/c2/c2.txt
./c/c1/c1.txt
./b/b1/b1.txt
./b/b.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 '17 at 4:58
  • try the above one. – Kamaraj Nov 6 '17 at 6:41
  • The files are repeating twice let me explain you the process. – absar Nov 6 '17 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 '17 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 '17 at 4:25

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.