LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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-12-2008, 11:32 PM   #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-12-2008, 11:39 PM   #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-12-2008, 11:41 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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-12-2008, 11:49 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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, 01: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, 02:39 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

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



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 09:57 AM
can 'ls' command be recursive? Hailey's_Comet Linux - Newbie 3 10-01-2006 12:29 AM
alias cs='cd $1; ls' milanche Linux - Newbie 11 09-13-2005 05:57 PM
Recursive grep jimieee Linux - General 5 10-06-2003 10:13 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:24 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
Open Source Consulting | Domain Registration