Bernardo Teixeira Bernardo Teixeira - 1 month ago 9
Bash Question

shell script -S and -Sd code not working correctly

I have the following code:

#Option -s for each set of files with the same name, presents sorted by size#

elif [ $1 = "-S" ]
then
shift
for i in $*
do
find $i -type f -print|while read F1
do

basename "${F1}"
done | sort | uniq -d | while read F2
do
find $i -type f -name "${F2}" -exec ls -l -S -R '{}' \;
done
done

#Option -d performs sorting in descending order.

elif [ $1 = "-d" ]
then
shift
for i in $*
do
find $i -type f -print|while read F1
do

basename "${F1}"
done | sort | uniq -d | while read F2
do
find $i -type f -name "${F2}" -exec ls -l '{}' \;
done
done


I was supposed to print them by size with the command
-S
and then in descending order with
-Sd
. My output doesn't seem to do that. At this point I can't figure out what is wrong in my code.

-rw-r--r-- 1 quetzal quetzal 33 Out 7 20:27 aa/cc/t1
-rw-r--r-- 1 quetzal quetzal 8 Out 7 20:36 aa/t1
-rw-r--r-- 1 quetzal quetzal 0 Out 8 08:16 aa/cc/t2
-rw-r--r-- 1 quetzal quetzal 0 Out 8 08:15 aa/t2
-rw-r--r-- 1 quetzal quetzal 16 Out 8 08:16 aa/cc/t4
-rw-r--r-- 1 quetzal quetzal 0 Out 10 12:53 aa/dd/t4

Answer

Reverse the sort like this:

elif [ $1 = "-d" ]
then
shift
for i in $*
do
    find $i -type f -print|while read F1
        do

            basename "${F1}"
        done | sort -r | uniq -d | while read F2
        do
                find $i -type f -name "${F2}" -exec ls -l '{}' \;
        done        
    done
Comments