Shell script for sorting files with same extension(.xml,.dat etc..)in a directory in
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Shell script for sorting files with same extension(.xml,.dat etc..)in a directory in
Hi,
Pls help me in writing a shell script for sorting the below files w.r.t latest date.
here date is within the file name, not according to the system date.
for example given files:
format is XXXXXX_YYYYMMDDHHMMSS-RequestID.xml
pls help me writing a shell script for sorting files with same extension(.xml,.dat etc..)in a directory in a sequence based on the timestamp, yyyyMMddhh[24hr]mmss.
You need to learn how to use ls with the -t flag. You also need learn about shell globing.
Quote:
Originally Posted by rsnkumar
I have zero knowledge on shell scripting..
Time to learn then.
Quote:
Originally Posted by rsnkumar
Pls provide the whole structure of the script, not only the logic..
So you want to sort them my name.. that is the default sort order for both a shell glob and ls. Eg
Code:
ls -1
However I'm beginning to think that I really don't understand what you are trying to do. "Sorting files"? In what context? I think you need to give the bigger picture about what you are actually trying to achieve.
Sorting order should be w.r.t latest date first.The order should be:
20140708_111740-abc.xml
20140705_112550-def.xml
20140710_111936-xyz.xml(latest date should appear last)
Now, I need to check the date and time stamp as available in the file names (and not the unix system date timestamp), compare it, and pick up the file with the lowest date timestamp in its name, first and latest date at last.
Sorting order should be w.r.t latest date first.The order should be:
20140708_111740-abc.xml
20140705_112550-def.xml
20140710_111936-xyz.xml(latest date should appear last)
That is *EXACTLY* the output you would get from
Code:
ls -1
or
Code:
echo * | tr ' ' '\n'
Quote:
Now, I need to check the date and time stamp as available in the file names (and not the unix system date timestamp), compare it,
compare it to what?
Quote:
and pick up the file with the lowest date timestamp in its name, first and latest date at last.
Code:
first=$(echo * | tr ' ' '\n' | head -1)
last=$(echo * | tr ' ' '\n' | tail -1)
echo "The first file is ${first}"
echo "The last file is ${last}"
I have to admit I'm pretty lost. I really don't know what you are trying to do. Again, giving the bigger picture would help. I suspect if you learn just a little bit of how to use the command line you'd be trivially able to do what you want, but since I don't understand what you are trying to do I don't think I can help.
Pls help me in writing a shell script for sorting the below files w.r.t latest date.
here date is within the file name, not according to the system date.
for example given files:
format is XXXXXX_YYYYMMDDHHMMSS-RequestID.xml
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.