3

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 ?

4

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.

  • 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
2

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

  • 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
-1

Use the tree command

tree -i |tail

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

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.