LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   subtract total numbers of files from 2 directories (https://www.linuxquestions.org/questions/linux-newbie-8/subtract-total-numbers-of-files-from-2-directories-715173/)

Michael235 03-28-2009 05:35 PM

subtract total numbers of files from 2 directories
 
Hello, I am storing the number in a file but the problem is that I don't know how to do the calculation from those two directories.

here is my program:

echo "enter first directory name"
read dir1

echo "enter second directory name"
read dir2

ls $dir1 | wc -l > r
ls $dir2 | wc -l > t

cat r
cat t

#let y=r+t I had error in this part I'm stuck here :scratch:
#echo y

sorry for my poor english...

i92guboj 03-28-2009 05:44 PM

Quote:

Originally Posted by Michael235 (Post 3490985)
ls "$dir1" | wc -l > r
ls "$dir2" | wc -l > t

Use quotations, otherwise you are going to have a bad time if the directory has spaces in its name.

Quote:

cat r
cat t

#let y=r+t I had error in this part I'm stuck here :scratch:
#echo y
Is there any reason why you use files to store the numbers? If there's one, then you will need to inline the cat's so they are expanded in the same formula, like this.

Code:

let y=$(cat r)+$(cat t)
But, what I'd do is to store the numbers in variables instead, so my version of the program would be more like this:

Code:

r=$(ls dir1 | wc -l)
t=$(ls dir2 | wc -l)
let y=$r+$t


Michael235 03-28-2009 05:49 PM

thank you
 
It works!!!
Thanks man!!!


All times are GMT -5. The time now is 10:48 PM.