count files recursively


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting count files recursively
# 1  
Old 05-05-2011
count files recursively

Hi,
New to shell scripting.
I am trying to count number of files in a directory that contains lot of sub-directories. Any input on this greatly appreciated.
thank you!
# 2  
Old 05-05-2011
Have you looked at the du command?

or

do a
Code:
ls -l *

# 3  
Old 05-05-2011
Try:

Code:
find . -type f | wc -l

# 4  
Old 05-05-2011
Hi,

I tried

find /dir_name -type f | wc -l

but this also counts hidden(.) files that i don't want.
# 5  
Old 05-05-2011
Code:
find . -type f ! -name ".??*" | wc -l

---------- Post updated at 11:04 PM ---------- Previous update was at 10:35 PM ----------
Code:
find . -type f ! -name ".*" | wc -l

# 6  
Old 05-09-2011
Hi ctsgnb,
thanks for ur input.

I am trying to find number of files in a directory that contains subdirs.
script should go to a directory in a given path count no of files in it and print no of files along with last modification date and then come out of that dir and go to next dir and do the same and print output. I tried the following script but obviously its not getting right results. Can anyone tell me how to achieve this.

code that i am trying with is: (i am sure this is messy as i am new to scripting)

Code:
#!/bin/bash
for file in /home/admin
do
 if [ -d "$file" ];then
   cd $file
   for file in $PWD
   do
     if [ -d "$file" ];then
       echo -n "File count within directory $file is:"
       find $file -type f ! -name ".*" | wc -l
       echo "last modification time of $file : $(stat -c %y $file)"
     else
       echo "Skipping $file since not a directory"
       cd ..
     fi
   done
 fi
done


Last edited by lramsb4u; 05-13-2011 at 11:41 AM.. Reason: Please indent your code and use code tags
# 7  
Old 05-09-2011
Try this :

Code:
cd /home/admin
ls -Rl1 | awk -F: 'NF==2&&!($2){x=$1;s[x]=0;next}/^-/{s[x]++}END{for(i in s){print "The dir " i " has " s[i] " files and was last modified :" ;system("stat -c %y " i);print}}'

If you are running on SunOS/Solaris plateform, use "nawk" instead of "awk"

Or something like (you may need to make the code more elegant but):
Code:
DIR=/home/admin
find $DIR -type d >>/tmp/dirlist
find $DIR -type f ! -name ".*" >>/tmp/flist
for i in `cat /tmp/dirlist`
do
    n=$(( $(egrep -e "^$i" /tmp/flist | wc -l) - 1 ))
    l=$(stat -c %y "$i")
    echo "Dir $i has $n file and were last modified $l"
done
rm /tmp/dirlist /tmp/flist

or you are looking for something like

Code:
count=0
sum(){
cd $1
ls | while read a
do
if [[ -f "$a" ]]
then let count+=1
elif [[ -d "$a" ]]
then let count+=$(sum "$a")
fi
done
echo $count
}
find . -type d | while read b
do
echo $b has $(ksh k $b) files and were last modified $(stat -c %y $b)
done


Last edited by ctsgnb; 05-09-2011 at 06:56 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Error files count while coping files from source to destination locaton as well count success full

hi All, Any one answer my requirement. I have source location src_dir="/home/oracle/arun/IRMS-CM" My Target location dest_dir="/home/oracle/arun/LiveLink/IRMS-CM/$dc/$pc/$ct" my source text files check with below example.text file content $fn "\t" $dc "\t" $pc "\t" ... (3 Replies)
Discussion started by: sravanreddy
3 Replies

2. UNIX for Dummies Questions & Answers

Copy files recursively

Hello! I know what i s recursion, but can't imagine what shoudl be "recursicve copying" of files? Please, what should mean: cp -r /home/hope/files/* /home/hope/backup Can someone helpme with a simple example? Many thanks!!! (6 Replies)
Discussion started by: pinklemon
6 Replies

3. Linux

Search files recursively

grep pattern filename To search for the pattern in all files in the current directory and the sub-directories recursively, what needs to be substituted in filename? (1 Reply)
Discussion started by: ravisingh
1 Replies

4. Shell Programming and Scripting

Recursively rename some files

Hello, I have one directory with 3 level sub-directories, and about houndard files under those directories. I need a shell script to rename all patern mateched directories and files. For example: the patern is AA in the directory or file name. Orignal directory:... (2 Replies)
Discussion started by: mail4mz
2 Replies

5. Shell Programming and Scripting

Recursively move directories along with files/specific files

I would like to transfer all files ending with .log from /tmp and to /tmp/archive (using find ) The directory structure looks like :- /tmp a.log b.log c.log /abcd d.log e.log When I tried the following command , it movies all the log files... (8 Replies)
Discussion started by: frintocf
8 Replies

6. UNIX for Dummies Questions & Answers

Need help in moving files recursively

Hi, I have d1,d2,d3 directories / /home/abc/d1 /home/abc/d2 /home/abc/d3 d1,d2 and d3 also have subdirctories. d1-->d11-->d12 d2-->d22-->d23 d3-->d33-->d34 All these directories have files like date_filename.txt so I want to find the files recusively for a particular date from... (1 Reply)
Discussion started by: jagadish_gaddam
1 Replies

7. Shell Programming and Scripting

Rename files recursively

hi I have files named 123_234_aaa.jpg 234_231_345.jpg and i wish to rename these files to aaa.jpg and 345.jpg. i.e inital number,_,next number should be removed form the file name. Please let me know how can i do it. (2 Replies)
Discussion started by: vasuarjula
2 Replies

8. Shell Programming and Scripting

Copy only files recursively

Hi, find . | xargs -s 47518 can list all the files and directories recursively , is there any possibility to copy only files from directories and subdirectoreis once it is listed. Please help Thans & Regards Uma (3 Replies)
Discussion started by: umapearl
3 Replies

9. UNIX for Dummies Questions & Answers

recursively renaming files

Hi, I'm new linux user.I need to recursively rename all files .I used this command "rename .MP3\;1 .MP3 *.MP3\;1" to rename files in one directory.But Inside this directory lots of sub directories there. Please help me out how to rename all files recursively? Thanks Govindan (2 Replies)
Discussion started by: catgovind
2 Replies

10. UNIX for Dummies Questions & Answers

List Files Recursively

Hi! I'd like to list my files recursively BUT: I want them in this format, so that I can use them as options for commands like ftp->put or del ./directory1/file1.tar ./directory1/file2.tar ./directory1/file3.tar ./directory2/file1.tar ./directory2/file2.tar ./directory2/file3.tar... (9 Replies)
Discussion started by: roberthawke
9 Replies
Login or Register to Ask a Question