0

This is my first post on StackOverflow, so please be gentle.

What I am trying to do is to sort the output of find by size (from greatest to smallest).

I have a file.txt which contains:

file1
file2
file3
file4
file5
file6

My code looks like this:

for x in $(cat file.txt)
do
find -name $x -printf '%s %p\n'| sort -rn
done

But instead the output is not sorted.

The output is the path of the files (which is great) and their sizes in the same order as in the file.

For example:

 128  /dir1/file1
 8 /dir2/file2
 0 file3
 4 file4
 ...
 and so on
legoscia
  • 39,593
  • 22
  • 116
  • 167
Alexander
  • 3
  • 1
  • Your loop works on each line in the text file. So you are passing `sort` 1 file each time, in the order of the text file. Sorry can't suggest how to fix it, but hope that helps understand whats going on. I would guess an array might help http://stackoverflow.com/questions/10981439/reading-filenames-into-an-array – JeffUK May 15 '17 at 14:28

1 Answers1

0

Your call to find prints a single line for each file, which you then send to sort. Since sort only gets a single line each time you call it, there is nothing to sort, and the files are output in the order they are listed in in file.txt.

Instead, pipe the output from the entire loop to sort:

for x in $(cat file.txt)
do
find -name $x -printf '%s %p\n'
done| sort -rn
legoscia
  • 39,593
  • 22
  • 116
  • 167