LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-13-2008, 12:32 AM   #1
SirTristan
Member
 
Registered: Feb 2006
Distribution: GNU/Linux
Posts: 54

Rep: Reputation: 15
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?
 
Old 03-13-2008, 12:39 AM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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.
 
Old 03-13-2008, 12:41 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
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
 
Old 03-13-2008, 12:49 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Code:
find . -iname \*gif -ls | sort -k 7,7 -k 11,11
maybe?

Finds all, sorts by size, then directory/name



Cheers,
Tink
 
Old 03-13-2008, 02:06 PM   #5
SirTristan
Member
 
Registered: Feb 2006
Distribution: GNU/Linux
Posts: 54

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jschiwal View Post
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 View Post
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.?
 
Old 03-13-2008, 03:39 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
recursive grep xpucto Solaris / OpenSolaris 2 05-29-2007 10:57 AM
can 'ls' command be recursive? Hailey's_Comet Linux - Newbie 3 10-01-2006 01:29 AM
alias cs='cd $1; ls' milanche Linux - Newbie 11 09-13-2005 06:57 PM
Recursive grep jimieee Linux - General 5 10-06-2003 11:13 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM


All times are GMT -5. The time now is 05:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration