Need help in sorting the file and copying

Hello Experts,

I have a requirement:

In a directory, four files are getting created on monthly basis, having timestamp in the end of every file. I want to arrange file in ascending order and copy the first file to other directory and after copying i want to remove it.

Example
-----------
Test_Directory
-File06302011
-File07312011
-File08312011
-File09302011

Copy File06302011 to some other location and after copying remove it from Test_Directory.

Please help

When you say the files are getting created monthly basis, does it mean that each file created every month? or all of them created at once one for each month?

--ahamed

1 Like

Instead of copying and removing .. just move the file to the destination path ..

$ ls File* | sort -n | nawk '{if(NR==1) {print "mv "$0" /dest/path"}}' | sh
1 Like

If the files maintain the timestamp then try this...

ls -t Test_Directory/FILE* | tail -1 | xargs -I {} mv {} /your/destination

--ahamed

1 Like

Hi Ahamed,

Yes, the file is getting created on monthly basis....every month end a new file will be created and I have to move the oldest file to some other location.

Thanks a lot experts...

Base on the file created each month, you can put below command in your cronjob, and it will automatically move the old files out of the test_directory.

find /test_directory -type f -mtime +90 -exec mv {} /new folder \;