Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I was taking a backup of a folder, where I used the following command :

sudo tar -zcvf www.tar.gz /var/www/

It successfully compressed, just for verifying the number of files in the compressed archive, and the directory I ran the following commands :

sudo tar -ztvf www.tar.gz | wc -l -> 186364

ls -R /var/www | wc -l -> 196440

How come the count is different, what am I missing ?

share|improve this question
add comment

3 Answers 3

up vote 4 down vote accepted

Look at the output without piping to wc to see the difference

$ ls -R www | cat
www:
dir1
dir2
file1

www/dir1:
file1

www/dir2:
file1
file2
$ ls -R www | wc -l
11

And

$ tar xvfz www.tar.gz
www/
www/dir1/
www/dir1/file1
www/dir2/
www/dir2/file1
www/dir2/file2
www/file1
$ tar xvfz www.tar.gz |wc -l
7

ls -R just produces some more output.

share|improve this answer
    
Yup, never realized that ls adds new lines between folders. –  Aaditi Sharma Dec 9 '12 at 8:39
1  
Just verified the number of files using find /var/www/ -type 'd' -o -type 'f' | wc -l –  Aaditi Sharma Dec 9 '12 at 8:41
add comment

ls -R puts some extra empty lines in the output which causes the count to increase.

You can test it by running the command ls -R without piping it to wc -l

share|improve this answer
    
Thanks, yup that was the problem. Would upvote, however, still require 15 rep. –  Aaditi Sharma Dec 9 '12 at 8:42
    
@AaditiSharma: Its okay aaditi :P –  pradeepchhetri Dec 9 '12 at 8:51
add comment

Use the tree command

tree -i |tail

this will give you the number of folders and files in particular folder.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.