How to find files larger than one size and sort them by the date of the last change?

I need to write a script that writes each file in a selected directory that is larger than a certain size. I also need to sort them by size, name and date of last modification.

So, I made the first two cases:

Sort by size

RESULTS=`find $CATALOG -size +$SIZE | sort -n -r | sed 's_.*/__'` 

Sort by name

 RESULTS=`find $CATALOG -size +$SIZE | sed 's_.*/__' | sort -n ` 

But I do not know how to sort the results by the date of the last change.

Any help would be appreciated.

+6
source share
2 answers

One of the best approaches, provided you have too many files, is to use ls for the sort itself.

Sort by name and print one file per line:

 find $CATALOG -size +$SIZE -exec ls -1 {} + 

Sort by size and print one file per line:

 find $CATALOG -size +$SIZE -exec ls -S1 {} + 

Sort by modification time and print one file per line:

 find $CATALOG -size +$SIZE -exec ls -t1 {} + 

You can also play with the ls switches: Sort by modification time (small at first) with a long listing format with human-readable sizes:

 find $CATALOG -size +$SIZE -exec ls -hlrt {} + 

Oh, you may want to only find files (and ignore directories):

 find $CATALOG -size +$SIZE -type f -exec ls -hlrt {} + 

Finally, some notes: avoid capitalized variable names in bash (this is considered bad practice) and avoid reverse ticks, use $(...) instead. For instance.

 results=$(find "$catalog" -size +$size -type f -exec ls -1rt {} +) 

Also, you probably don't want to put all the results in a row, as in the previous row. You probably want to put the results in an array. In this case, use mapfile as follows:

 mapfile -t results < <(find "$catalog" -size +$size -type f -exec ls -1rt {} +) 
+11
source

Try xargs (do something by treating STDIN as a list of arguments) and the -t and -r flags on ls . i.e. something like this:

 find $CATALOG -size +$SIZE | xargs ls -ltr 

This will give you files sorted by last modification date.

Sorting by several attributes at once will be very inconvenient to do with utilities and shell pipes - I think you will need to use a scripting language (ruby, perl, php, whatever), unless your fu shell is strong.

+1
source

All Articles