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 |
something like
Code:
ls /path/to/dir/ | sort -t. -k2,2 -rg | head -n1 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 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 |
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". |
If the number is the only dynamic part and no leading zero's are used:
Code:
ls -v file*res | tail -1 |
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` ../ |
Quote:
Code:
cp "$(ls -v file*res | tail -1)" /path/to/destination/ |
Works Smoothly. Thanks.
|
All times are GMT -5. The time now is 02:01 PM. |