Copy and Delete Question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy and Delete Question
# 1  
Old 10-03-2008
Copy and Delete Question

I have a script to tar up some older files. It did a find and mv, but I had to change it to preserve the directory structure because some of the directories now have duplicate nameed files. I changed it to a find and copy, then added a find and remove.

Is there a better way so I don't have to do 2 finds? I tried to do the tar in the find, but it ignored the directory structure and overlaid the duplicate named files.

/opt/applic/AAA/logs/ and /opt/applic/BBB/logs/ each have a file called output.log

TARGETDIR=/opt/applic/*/logs/
# WORKDIR/REF is a reference file dated to be the oldest file we will keep
find $TARGETDIR/ -type f -maxdepth 1 -iname \*.log\* ! -newer $WORKDIR/$REF -exec cp -p --parents {} $WORKDIR \;
find $TARGETDIR/ -type f -maxdepth 1 -iname \*.log\* ! -newer $WORKDIR/$REF -exec rm -f {} \;
tar cvf ARCH/logs_xyz.tar $WORKDIR
# 2  
Old 10-04-2008
You can have multiple -exec commands in the same find. Or you could -exec a simple script which copies and removes only if the copy was successful.

Code:
find $TARGETDIR/ -type f -maxdepth 1 -iname \*.log\* ! -newer $WORKDIR/$REF \
  -exec sh -c 'cp -p --parents {} $WORKDIR && rm -f {}' \;

# 3  
Old 10-06-2008
Thanks

That worked after I removed the \ after $REF.

I had tried to do multiple -exec but could not get the code to recognize it correctly. Your solution worked great!

Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with script to copy/rename files, then delete by date

Hi All, I am new to scripting and am looking for some assistance setting up a script. Basically I need the script to scan a folder for the newest files and make a copy of those files, adding a month to the date stamp. I also need this script to delete the previously copied files to save space.... (4 Replies)
Discussion started by: Lucid13
4 Replies

2. Shell Programming and Scripting

copy, then delete lines in file with sed using a pattern

I need to copy lines to a new file from files with sed using a pattern in char postions 1-3. Then after the copy, I need to delete those same lines from the input files. For example, string "ABC" in pos 1-3 (6 Replies)
Discussion started by: laksjfhoius9123
6 Replies

3. Shell Programming and Scripting

Loop folders, delete files, copy new ones

Folks, I am hopeful that you may be able to help me out with writing a script that can be run nightly (as cron?) to loop through all subfolders within the "/media" directory, delete all of the files in each of them, and then copy in all of the files from the "/home//sansa" directory to each of... (6 Replies)
Discussion started by: acraig
6 Replies

4. Shell Programming and Scripting

Using RCP to copy and delete the files from Windows Drive

Hi, I have a requirement to copy the files from C drive on Windows to UNIX, once the files are copied I need to delete them from that drive (C:). A drive is also on same network as my unix, so I was asked to use RCP for copying the files. Can any one have the syntax to copy the files from... (0 Replies)
Discussion started by: Raamc
0 Replies

5. Shell Programming and Scripting

Script required to copy and delete files

Hi All, I have written a script to ensure all my ARCHIVE files are backed up retaining the directory structure. The Script is-- #!/bin/sh # Script to take backup of DCS Folders # The file locations are /ftp/edi_ftp/edi_p/dcsftp/DCS/*/ARCHIVE DCSITIME=$(perl -e '($ss, $mm, $hh, $DD, $MM,... (3 Replies)
Discussion started by: srnagu
3 Replies

6. Solaris

Delete and copy file(s) slowly(!?)

Hi all! I have to monitor space in V890 machine, Solaris 10 weekly, because there is Oracle DB on it with many datafiles which have been taken offline to make enough size. Sometime, one or more datafiles are big, they are 20GB, 40GB etc.. The problem I have encountered is the processing of... (5 Replies)
Discussion started by: trantuananh24hg
5 Replies

7. Shell Programming and Scripting

How to copy to server & delete locally?

Hi, I've got a cronjob running daily creating backup files. How can I send that created file to a remote server & then delete it on my workstation to save space? The filename format is as follows: proj_bk_20070624_15h30, proj_bk_20070625_15h30, etc tar cvf - /project | gzip -c > proj_bk_$(date... (3 Replies)
Discussion started by: mokgonec
3 Replies

8. Shell Programming and Scripting

Unzip, copy, and delete loop

Hey there, I am trying to move zipped text files from a remote server to a remote tape storage facility, through my home directory. What I want to do is get the zip file (using scp), unzip it, copy the output text file which was inside (using rfcp) to the tape storage server, and then delete... (3 Replies)
Discussion started by: spyne
3 Replies

9. UNIX for Dummies Questions & Answers

Telnet and file Copy/Delete Problem

Hi there....I'm new for the UNIX... just wondering if there is any method that can telnet to a server without typing the userID and PWD each time... that is any command or scripts that allows me to enter the server directly? also...after i enter the server... i want to get some files then... (17 Replies)
Discussion started by: biglemon
17 Replies
Login or Register to Ask a Question