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 ?

3 Answers 3

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.

2
  • Yup, never realized that ls adds new lines between folders. Dec 9, 2012 at 8:39
  • 1
    Just verified the number of files using find /var/www/ -type 'd' -o -type 'f' | wc -l Dec 9, 2012 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

2
  • Thanks, yup that was the problem. Would upvote, however, still require 15 rep. Dec 9, 2012 at 8:42
  • @AaditiSharma: Its okay aaditi :P Dec 9, 2012 at 8:51
-1

Use the tree command

tree -i |tail

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .