LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 09-16-2009, 10:16 AM   #1
cylon
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Rep: Reputation: 0
Find command and outputting to an existing tar file


I need help with a command. finding all files created by cylon over 5 days old. add those files to an existing tar file and then delete the files.

When i output with find, its giving me an output of the file names not the actual files. How do i actually get the files copied into the existing tar file?

I can google and see how to add files to an existing tar file. and I know to use the find command to find the files, but how can i pipe the actual files into the tar file and then delete the source files, particularly without doing this one file at a time.

with find i was thinking the command would be something like find * -user cyclon -mtime +5. How do I then take the actual files that find locates, add them to an existing tar file /home/cylon/mybkup and then delete them.

Any help is appreciated.
 
Old 09-16-2009, 10:19 AM   #2
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by cylon View Post
I need help with a command. finding all files created by cylon over 5 days old. add those files to an existing tar file and then delete the files.

When i output with find, its giving me an output of the file names not the actual files. How do i actually get the files copied into the existing tar file?

I can google and see how to add files to an existing tar file. and I know to use the find command to find the files, but how can i pipe the actual files into the tar file and then delete the source files, particularly without doing this one file at a time.

with find i was thinking the command would be something like find * -user cyclon -mtime +5. How do I then take the actual files that find locates, add them to an existing tar file /home/cylon/mybkup and then delete them.

Any help is appreciated.

one way -

Code:
find / -user cyclon -mtime +5 -exec 'rest of command goes here' {} \;

Last edited by centosboy; 09-16-2009 at 10:22 AM.
 
Old 09-16-2009, 11:14 AM   #3
cylon
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Original Poster
Rep: Reputation: 0
I'm still not clear. I understand that this finds the file and attempts to execute something.

What I don't understand is how to output the actual files that "find" discovers and output them into the existing tar file. Once the files are in the tar file, the source files should be deleted.
 
Old 09-16-2009, 12:06 PM   #4
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by cylon View Post
I'm still not clear. I understand that this finds the file and attempts to execute something.

What I don't understand is how to output the actual files that "find" discovers and output them into the existing tar file. Once the files are in the tar file, the source files should be deleted.
This should work:

Code:
path="/path/of/interest"

find $path -type f -user cylon -mtime +5 | while read filepath
do
   echo "Processing $filepath."
   echo "Adding $filepath to tar archive..."
   if tar archive-name --append $filepath
   then
      echo "Successfully added $filepath to archive."
      echo "Deleting $filepath..."
      if rm -f $filepath
      then
         echo "Successfully deleted $filepath"
      else
         echo "Error: could not delete $filepath"
      fi
   else
      echo "Error: could not add $filepath to archive."
   fi
done
But bear in mind these warnings:

1. The file filtering code finds all files older than five days, but it doesn't distinguish between ordinary data files and system files that might reside in the user's directory (and that shouldn't be deleted). A more sophisticated filtering method would be required to filter only normal data files, not system files (like Gnome and KDE configuration files, for example).

2. I haven't actually created an archive and appended files to it, to see what happens. So you need to actually test this method carefully on copies of example files, not the originals. My untested tar syntax might be wrong -- tar is a rather complex bit of code. Maintain full backups during testing.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C programming problem, fopen() cannot find existing file Kenarkies Linux - Software 2 07-30-2009 02:59 AM
tar file without remove existing data ust Linux - Software 3 06-18-2009 01:26 AM
tar file without remove existing data ust Linux - Newbie 3 06-10-2009 02:55 AM
where to find a tar-file in a rpm sneezesnoeze Linux - Newbie 1 04-06-2004 12:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration