LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Sorting recursive 'ls' and 'grep' (https://www.linuxquestions.org/questions/linux-newbie-8/sorting-recursive-ls-and-grep-627669/)

SirTristan 03-12-2008 11:32 PM

Sorting recursive 'ls' and 'grep'
 
If you do for instance the following:
Code:

ls -RSl images | grep '.gif'
It will sort by file size. However the recursion is not done before the sorting - sorting will be grouped by subfolder, not consolidated.

Is there any way to sort in a consolidate fashion? Where the recursion to gather the file data is done first, and then all files are sorted together?

anomie 03-12-2008 11:39 PM

Could this work for your situation?

$ find . -iname '*.gif' -exec basename {} \; | sort

i.e. Find the .gifs, get the basename (filename minus directory), then pipe it all to sort.

jschiwal 03-12-2008 11:41 PM

Do you want to sort simply by the basename? You could use find instead. This example prints two entries per file, the basename and the full pathname and then uses sort to sort the output:
Code:

find ./ -type f -printf "%f\t%p\n" | sort

Tinkster 03-12-2008 11:49 PM

Code:

find . -iname \*gif -ls | sort -k 7,7 -k 11,11
maybe?

Finds all, sorts by size, then directory/name



Cheers,
Tink

SirTristan 03-13-2008 01:06 PM

Quote:

Originally Posted by jschiwal (Post 3087012)
Do you want to sort simply by the basename? You could use find instead. This example prints two entries per file, the basename and the full pathname and then uses sort to sort the output:
Code:

find ./ -type f -printf "%f\t%p\n" | sort

I want to sort by file size, and also sometimes by modification date. And so based on that, this works for file size:
Code:

find ./ -iname '*.gif' -type f -printf "%s\t%p\n" | sort -r -n
And also for sorting by modification change time:
Code:

find ./ -iname '*.gif' -type f -printf "%T@\t%t\t%p\n" | sort -r -n
Quote:

Originally Posted by Tinkster (Post 3087018)
Code:

find . -iname \*gif -ls | sort -k 7,7 -k 11,11
maybe?

Finds all, sorts by size, then directory/name



Cheers,
Tink

Yes that works :) (with -r added to sort) One problem is that filesize is printed in 1K blocks.

I prefer the find...-ls format to using -printf, but can -ls format be done with raw file size rather than file size in 1K blocks?

And is there any way to use the -ls format to sort by modification change time/status change time etc.?

Tinkster 03-13-2008 02:39 PM

In that case all you need to do is to invoke the *real* ls rather that using
finds built-in one. re-apply your -r to sort where ever needed ;}
Code:

find . -iname \*gif -exec ls  -lc {} \;| sort -k 7,7 -k 11,11

Cheers,
Tink


All times are GMT -5. The time now is 03:46 AM.