Reputation: 69
I'm making a shell script to search for files with a specific name and show their full path and size.
For example:
/home/miglui/Desktop/SO/teste/1/teste.txt: 14 bytes
The code of the segment that I'm having trouble is the next:
for i in `find $1 -name $4 -type f -printf "%s "` ; do
path=`readlink -f $4`
echo "$path: $i bytes"
done
The code returns:
/home/miglui/Desktop/SO/teste.txt: 14 bytes
/home/miglui/Desktop/SO/teste.txt: 48 bytes
/home/miglui/Desktop/SO/teste.txt: 29 bytes
But should return:
/home/miglui/Desktop/SO/teste/1/teste.txt: 14 bytes
/home/miglui/Desktop/SO/teste/2/teste.txt: 48 bytes
/home/miglui/Desktop/SO/teste/teste.txt: 29 bytes
What can be the problem?
Upvotes: 0
Views: 373
Reputation: 181104
The problem is that every iteration of the loop prints argument 4 ($4
) of the script. That has nothing to do with the results of your find
. Perhaps you want something more like this:
while read size name; do
path=`readlink -f $name`
echo "$path: $size bytes"
done < `find $1 -name $4 -type f -printf '%s %h/%f\n'`
Upvotes: 1
Reputation: 247042
You are retrieving the size of 3 different files, but only reporting the name of the parameter you pass in.
Try this:
( cd -P -- "$1" && find "$(pwd -P)" -name "$4" -type f -printf "$p: %s bytes\n" )
Upvotes: 0