LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-04-2013, 11:51 AM   #1
rpd25
LQ Newbie
 
Registered: Dec 2011
Posts: 12

Rep: Reputation: Disabled
Finding largest file number in the pattern


Hi,

I need a help in finding the largest file number in the file pattern.
As example, the largest file number is file.153.res in the following file list.

file.0.res
file.1.res
file.2.res
file.3.res
...
file.153.res

Please let me know what linux script would find the largest pattern.
Thanks in advance.

Warm regards,
Rakesh
 
Old 08-04-2013, 12:31 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
something like

Code:
ls /path/to/dir/ | sort -t. -k2,2 -rg | head -n1
# or long format
ls /path/to/dir/ | sort --field-separator=. --key=2,2 --reverse --general-numeric-sort | head --lines=1
so we get sort to 'split' the input into 3 fields, do a numerical sort on field 2, reversed so highest is first then using head to limit output to the "first" line

I'm sure there are other ways


if you have control over the filenames, you could pad with 0, then ls would do the sort for you, ls -r | head -n1


assuming a simple count..
1 2 3 .. 100 .. 200
Code:
for i in {1..200};do
   FileName=file.$(printf "%03d" $i).res
done
will give you file.001.res , file.002.res

the above is a little redundant, since for i in {001..200} will achieve the same without the need to mess with printf,
just demonstrates how printf can help
 
Old 08-04-2013, 12:44 PM   #3
rpd25
LQ Newbie
 
Registered: Dec 2011
Posts: 12

Original Poster
Rep: Reputation: Disabled
Thank you for the quick reply.

The script works fine. However, I need to select the largest file number in "file.*.res" starting with the name "file".
 
Old 08-04-2013, 12:45 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394
If the number is the only dynamic part and no leading zero's are used:
Code:
ls -v file*res | tail -1
EDIT: You've asked this exact same question before: Finding max number in filename and opening it

Last edited by druuna; 08-04-2013 at 12:52 PM.
 
Old 08-04-2013, 01:12 PM   #5
rpd25
LQ Newbie
 
Registered: Dec 2011
Posts: 12

Original Poster
Rep: Reputation: Disabled
Sorry to bother. This is a very foolish question.

How to copy the file with largest number to other directory?
I tried using the following command but it didn't work
ls -v file*res | tail -1 | cp `ls` ../
 
Old 08-04-2013, 01:16 PM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394
Quote:
Originally Posted by rpd25 View Post
Sorry to bother. This is a very foolish question.

How to copy the file with largest number to other directory?
I tried using the following command but it didn't work
ls -v file*res | tail -1 | cp `ls` ../
Code:
cp "$(ls -v file*res | tail -1)" /path/to/destination/
 
1 members found this post helpful.
Old 08-04-2013, 01:27 PM   #7
rpd25
LQ Newbie
 
Registered: Dec 2011
Posts: 12

Original Poster
Rep: Reputation: Disabled
Works Smoothly. Thanks.
 
  


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
[SOLVED] find largest number in file? qrange Linux - Newbie 5 04-09-2012 07:39 AM
Finding the right file, with only a line and column number Fliggerty Linux - Software 3 08-01-2011 07:27 PM
Help finding three largest files on system F4U57 Linux - Newbie 4 06-08-2010 04:04 AM
Please chech my statement - delete all except file with largest number extension laki47 Linux - Newbie 3 02-20-2009 07:02 AM
Shell Script : Finding a duplicate Number from file ? avklinux Programming 8 12-16-2008 12:50 PM

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

All times are GMT -5. The time now is 12:23 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