Reputation: 369
I want to write a shell script that compares the sizes of two directories X and Y, and reports which directory has more files and which directory has more subdirectories. So X and Y are two arguments.
I know the code for counting files in a directory is
ls -l | wc -l
I was having some trouble with the arguments and comparisons.
Also the same thing for subdirectories. I am new to shell scripting so any help would be appreciated.
Upvotes: 1
Views: 308
Reputation: 6449
For files you can do:
filesX=$(find "${X}" -type f | wc -l)
filesY=$(find "${Y}" -type f | wc -l)
if (( filesX < filesY )); then
echo "${Y} has more files"
elif (( filesX > filesY )); then
echo "${X} has more files"
else
echo "${X} and ${Y} have same number of files"
fi
For dirs it's basically the same:
dirsX=$(find "${X}" -type d | wc -l)
dirsY=$(find "${Y}" -type d | wc -l)
if (( dirsX < dirsY )); then
echo "${Y} has more dirs"
elif (( dirsX > dirsY )); then
echo "${X} has more dirs"
else
echo "${X} and ${Y} have same number of dirs"
fi
Upvotes: 1