Search subdirectories and find and print total files

Hi All,

I have a folder name lets say path/to/folder/CUSTOMER and under this i have several folders and each of these subfolder have serveral subfolders and so on and at some point i will have a folder name called "FTP_FILES" .

I need to search for these folders named "FTP_FILES and then print the path and number of *.txt files in it.

I have the following code by which i am able to find the folder and list *.txt the files but how do i print the count of file in each line.

find ./CUSTOMER -name 'FTP_FILES' -print0 | xargs --null ls *.txt

Output desired..

PATH of the FTP_FILES folder :
Number of *.txt files :

Please help

Hint: try using -type d option for directory

Thanks for you reply, I tried using -type d option but i'm getting the same result as before. my above command actually is finding the folder and the .txt files but i also the number of .txt files in each FTP_FILES directory. And also, i see "total 31088" in one of the directories where there is only one file. Can you please help me with this. I need to find .txt files in all these folders and print the count of .txt files.

find ./CUSTOMER -type d -name 'FTP_FILES' -print0 | xargs --null ls -l
./DIR1/DIR2/DIR3/DIR4/DIR5/FTP_FILES:
total 0

./DIR1/DIR2/DIR3/DIR4/DIR5/FTP_FILES:
total 0

./DIR1/DIR2/DIR3/DIR4/DIR5/FTP_FILES:
total 31088
-rw-r--r-- 1 user group 31832920 Aug 10 18:25 GRUN_IN_ALA_080813_105011.txt

./DIR1/DIR2/DIR3/DIR4/DIR5/FTP_FILES:
total 0

./DIR1/DIR2/DIR3/DIR4/DIR5//FTP_FILES:
total 0

Try this for finding the file count:

find ./CUSTOMER -type d -name 'FTP_FILES' -print 0 | ls -l *.txt | grep ^- | wc -l 

Thank you, I tried your suggestion below but i'm getting result as "1"

find ./CUSTOMER -type d -name 'FTP_FILES' -print0 | ls -l *.txt | grep ^- | wc -l
1

But when i run the below i get "32" !!

find ./CUSTOMER -type d -name 'FTP_FILES' -print0 | xargs --null ls -l | grep ^-|wc -l
32

Also, i just realised that in some FTP_FILES folders there are sub-directories named by date and the actual files are in the date directories. The above command just list the directories.. but i need those files to be displayed as well. i know i might be asking some basics here sorry i'm still learning.

So your requirement is to search for the FTP_FILES folder inside a given folder, and to traverse through all sub-folders in the FTP_FILES folder that is returned to find out *.txt files?

Yes Sir, find a FOLDER named FTP_FILES and show me all files actually (not just *.txt) and if a sub-directory exixts under FTP_FOLDER then show the files from those sub-directories as well.

K

In your first request you said, you want the count also. Is that so? Or you want only to display the files?

--ahamed

1 Like

Correct, The count also please. Find a FOLDER named FTP_FILES and show me all files actually (not just *.txt) and if a sub-directory exists under FTP_FOLDER then show the files from those sub-directories as well.

Print the path name of the FTP_FILES or Sub Directory if exists,
Print files available in that path
Print number of files in that path.
Print total number of files found from all paths.

K

Is this what you need?

find . -type d -name 'FTP_FILES' | xargs -I % sh -c 'echo dir:%; printf files: ; ls % | wc -l'
1 Like

Perfect thats what i needed. Appreciate you time and help.

Thanks a Ton