How to backup latest date files?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to backup latest date files?
# 1  
Old 10-04-2010
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,
Code:
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.

2. 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.

Last edited by Franklin52; 10-06-2010 at 03:26 AM.. Reason: code tags
# 2  
Old 10-04-2010
for files in dir but not in listfile use:
Code:
 ( cd dir ; ls ) | fgrep -vf listfile

For files in listfile but not in dir use:
Code:
 fgrep -v "$( cd dir; ls )" listfile

# 3  
Old 10-05-2010
1. find out the old files.

Code:
ls XX*.dat | sort -t_ -k2nr |sed -n '3,$p'

if it is fine, backup them.

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

2.
Code:
cd /CURRENT_DIR

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

# 4  
Old 10-05-2010
Quote:
Originally Posted by rdcwayx
1. find out the old files.

Code:
ls XX*.dat | sort -t_ -k2nr |sed -n '3,$p'

if it is fine, backup them.

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

2.
Code:
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 ----------

Quote:
Originally Posted by Chubler_XL
for files in dir but not in listfile use:
Code:
 ( cd dir ; ls ) | fgrep -vf listfile

For files in listfile but not in dir use:
Code:
 fgrep -v "$( cd dir; ls )" listfile

Thank you very much it is working fine Smilie Smilie
# 5  
Old 10-06-2010
Look carefully on my code. Your ll command with -l option already, that's why you get wrong command.

try to use
Code:
ls

# 6  
Old 10-06-2010
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

Code:
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?
# 7  
Old 10-06-2010
Quote:
Originally Posted by rdcwayx
Look carefully on my code. Your ll command with -l option already, that's why you get wrong command.

try to use
Code:
ls

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).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find latest date in folder

HI I have folder in home dir. /home/kpp/07222013 /home/kpp/07212013 /home/kpp/07202013 Output :-- /home/kpp/07222013 Just find latest date (5 Replies)
Discussion started by: pareshkp
5 Replies

2. UNIX for Dummies Questions & Answers

Selecting the file of latest Date

Hi Folks, I have one query that there is a folder in which daily several logs files are getting created , I reached to that location through putty but what I observer that 10 files of different date are been created with same name , what I need to see is the latest file ...let say the location is ... (5 Replies)
Discussion started by: KAREENA18
5 Replies

3. Shell Programming and Scripting

grep latest file based on date.

hi all, not sure if this has been posted b4 but i try to search but not valid. this is my question: when i do a ls -ltr there will be a list generated as follows: -rw-r--r-- 1 root sys 923260 Jan 10 04:38 FilePolling.41025.083TL021.xml -rw-r--r-- 1 root sys 1761337 Jan 10 04:40... (12 Replies)
Discussion started by: lweegp
12 Replies

4. UNIX for Dummies Questions & Answers

get the latest file by reading the date in the filename.

Hi, I grep for a pattern in a list of files. "grep -i -l $pattern *.datx*" it may give me n number of files. say for eg, it gives me 2 files. lock_eicu_20071228_00000000.dat_20071228_05343100 lock_eicu_20080501_00000000.dat_20080501_05343900 out of these 2 files I need to get the... (7 Replies)
Discussion started by: prsshini
7 Replies

5. Filesystems, Disks and Memory

backup of files for a specific date

In Linux Advance server I want to write one script for backing up files for a specific date like 24/07/2008. (3 Replies)
Discussion started by: akm9999
3 Replies

6. Shell Programming and Scripting

shell script to find latest date and time of the files

Hi everyone, Please help:) I have a list of 1000 different files which comes daily to the directory.Some of the files are not coming to the directory now. I need to write a shell script to find the latest date and time of the files they came to the directory. The files should be unique.... (1 Reply)
Discussion started by: karthicss
1 Replies

7. Shell Programming and Scripting

backup of files for a specific date

I want to write script for backing up archive logs files for specific date. please give me idea for that. (2 Replies)
Discussion started by: akm9999
2 Replies

8. Shell Programming and Scripting

Backup files per Date String

Guys, I've got a quick logic question. I'm pretty savvy when it comes to shell scripting, however, I'm at a loss when it comes to writing a simple shell script to backup only certain files out of the month. I have a directory, for example, /data/backups/websites/domain. In each "domain"... (5 Replies)
Discussion started by: drewrockshard
5 Replies

9. Shell Programming and Scripting

Loop through files in dir, omit file with latest date

I want to loop through files in a directory but omit the file with the latest date in my list of files. How would I accomplish this? Thanks (2 Replies)
Discussion started by: stringzz
2 Replies

10. UNIX for Dummies Questions & Answers

Copying files with the latest date

Hi All, I have a situation where I need to copy the files having the latest date. For example I have a file by name bas100e1_jun05. I need to copy it to bas100e1. But when a file by name bas100e1_jul05 is put in the same directory the script should copy the file having the latest month which... (34 Replies)
Discussion started by: shashi_kiran_v
34 Replies
Login or Register to Ask a Question