How to backup latest date files?

Hi,

 I have below two situations to handle,
  1. I have list of files with file names having date&time. I have to backup to old date files. say I have below files in a directory,
1. XX123_20101004010101.dat
2. XX124_20101004010201.dat
3. XX121_20101003010101.dat
4. XX122_20101002010101.dat

Now, I have to keep the files 1 and 2 and backup the files 3 and 4 into other dir.

  1. I have list of files in a dir and I have list of file names in a file xx123. I need to find files present in dir and not in xx123 and vice versa. is there any short way to do this?

Thanks,
Raja.

for files in dir but not in listfile use:

 ( cd dir ; ls ) | fgrep -vf listfile

For files in listfile but not in dir use:

 fgrep -v "$( cd dir; ls )" listfile
  1. find out the old files.
ls XX*.dat | sort -t_ -k2nr |sed -n '3,$p'

if it is fine, backup them.

ls XX*.dat | sort -t_ -k2nr |sed -n '3,$p' |xargs -i cp {} /OTHER_DIR
cd /CURRENT_DIR

diff <(sort XX123) <(ls XX*.dat|sort)

Thank you. The command is not exactly fetching the old files. Check the below,
$ ll XX*
-rw-r--r-- 1 fp015362 user 0 Oct 5 19:45 XX111_20101005010203.dat
-rw-r--r-- 1 fp015362 user 0 Oct 5 19:44 XX121_20101003010101.dat
-rw-r--r-- 1 fp015362 user 0 Oct 5 19:44 XX122_20101002010101.dat
-rw-r--r-- 1 fp015362 user 0 Oct 5 19:44 XX123_20101004010101.dat
-rw-r--r-- 1 fp015362 user 0 Oct 5 19:44 XX124_20101004010201.dat
$ ls XX*.dat | sort -t_ -k2nr |sed -n '3,$p'
XX123_20101004010101.dat
XX121_20101003010101.dat
XX122_20101002010101.dat

---------- Post updated at 05:53 AM ---------- Previous update was at 05:48 AM ----------

Thank you very much it is working fine :slight_smile: :b:

Look carefully on my code. Your ll command with -l option already, that's why you get wrong command.

try to use

ls

In (1) how do you know what files to backup? Is it just the file names (not the creation/modification time of the file)?

If so and you have gnudate, you can generate the cut-off timestamp (eg for 30 days) like this

date -d today-30days +%Y%m%d000000

Then pick up any file that has a timestamp less than your cut-off "20100906000000".
Is this on the right track?

I have used ll command just to show the available files. Pls check the next command to find the old files. latest files will be fetched using the date in the file name (time part needs to be ignored).