LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-21-2008, 07:34 AM   #1
SmurfGGM
Member
 
Registered: Jul 2008
Posts: 49

Rep: Reputation: 0
Using cpio to MOVE files


Howdy there

I want to be able to move files and there directory structure to another area.

I have been using CPIO ..

find . -type f -name "*rtn" -mtime +7 | grep -v archive | cpio -pvda arc

.. but this command only copy's the files & diretory structure to arc.

Is there an extra variable or a 'move' equivalent ?

Thanks in advance !
 
Old 07-21-2008, 08:08 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
What do you need copied besides the files and directories? It seems to work for me. What wasn't copied?

Look in the tar info manual. There is an example of running tar on the left of the pipe and in a subshell on the receiver end. You could use ssh to run one of them, easily transporting a large amount of files to another computer.
 
Old 07-21-2008, 09:22 AM   #3
SmurfGGM
Member
 
Registered: Jul 2008
Posts: 49

Original Poster
Rep: Reputation: 0
Smile

Thanks for that

The copy works, but I want to 'move' the files and there directory structure, ie

reports/Frank.Spencer/Betty to reports/archive/Frank.Spencer/Betty
 
Old 07-21-2008, 09:25 AM   #4
ajithender
Member
 
Registered: Jul 2007
Posts: 41

Rep: Reputation: 15
what about the mv command
 
Old 07-21-2008, 09:29 AM   #5
SmurfGGM
Member
 
Registered: Jul 2008
Posts: 49

Original Poster
Rep: Reputation: 0
mv wont preserve/create the directory structure
 
Old 07-22-2008, 03:21 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
Yes it will. Use the name of the directory as the "from" argument.
Suppose you have a directory: /media/disk/podcasts/. There are subdirectories in it as well. Running "mv /media/disk/podcasts ~/Documents/" will move the "podcast" directory and all of the files and directories to your Documents directory. As a matter of fact, if the source and destination are on the same filesystem, the only thing that happens is that the name of directory is changed from the source to destination. That is why you use "mv" to rename files.
 
Old 07-22-2008, 03:39 AM   #7
SmurfGGM
Member
 
Registered: Jul 2008
Posts: 49

Original Poster
Rep: Reputation: 0
Thanks for that, but the problem I have with this is, running the command .....

find . -type f -name "*rtn" -mtime +7 | grep -v archive | cpio -pvda arc

This searches the curernt directory (.) which is reports. This has 100's of sub directories, which can change each day, so I need this command ran every day. If I used the 'mv' command I would have to specify all of the 'from' direcctory names (I think).

Ta
 
Old 07-22-2008, 07:18 PM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
Yes, you could use the output of the find command in a for loop and A) take the directory name B) check if the directory exists C) create it if it doesn't exist D) copy the file to the destination.

However I don't see why your cpio command doesn't work. It worked for me when I tried it.
 
Old 07-22-2008, 08:12 PM   #9
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 62
You might find using tar a bit easier:

Code:
find /your/src/dir -type f '(' -name "*rtn" -and \! -path archive \) -mtime +7 | \
   tar -cvf - --files-from=- | \
      tar -xf - -C /your/dst/dir
I also combined your grep -v archive into find.

This doesn't delete your originals. You can add the --remove-files option to the first tar, but this is dangerous, since you cannot be sure the files are untar'd and safe on the other end until the command completes and the system has written the files physically to disk.

So it would be safer to remove all the files after the extraction tar has completed successfully. It would also be better to write the list of files to be moved into a file first, so that both the archive and removal use the same list. This requires three commands. First use find to determine your file list, storing that list to a file; second, use that file list with the creation side of tar (--files-from=yourlist), and third, use the same file list as a list of files to delete:

Code:
find /your/src/dir -type f '(' -name "*rtn" -and \! -path archive \) -mtime +7 > /tmp/files_to_move

tar -cvf - --files-from=/tmp/files_to_move | \
      tar -xf - -C /your/dst/dir

< files_to_move xargs rm -f
 
Old 07-23-2008, 02:20 PM   #10
kenoshi
Member
 
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159

Rep: Reputation: 32
Try:

tar cf - `find . -mtime +7 -type f -name '*.rtn' \! -path archive` | tar xvf - -C /path/to/arc/ | xargs rm

Hope this helps.
 
Old 07-23-2008, 02:56 PM   #11
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 62
Quote:
Originally Posted by kenoshi View Post
Try:

tar cf - `find . -mtime +7 -type f -name '*.rtn' \! -path archive` | tar xvf - -C /path/to/arc/ | xargs rm

Hope this helps.
This not only will fail when the ARGV limit is reached, but is VERY dangerous and will remove files even when they have not been copied.

Consider (my slightly changed version for testing):

Code:
$ tar cf - `find . -mtime +7 -type f -name '*.png' \! -path archive` | tar xvf - -C /tmp/test2 | xargs echo rm
gtar: ./graph.png: Cannot open: Permission denied
gtar: ./test.png: Cannot open: Permission denied
gtar: Error exit delayed from previous errors
rm ./graph.png ./test.png
Guess what - all the source files are now deleted, but no destination files exist. Short cuts in this case are not the correct way.
 
Old 07-23-2008, 03:15 PM   #12
kenoshi
Member
 
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159

Rep: Reputation: 32
Hehe, it was meant as a quick and dirty move without verification, goes without saying that you shouldn't do this for production or when moving large filesets

I make it a habit to do md5 comps before deleting in those situations, call me paranoid...
 
Old 07-23-2008, 03:22 PM   #13
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 62
That's all fine - but let's not give advice *without* the caveats about data loss. When quick and dirty is potentially catastrophic and can easily cause data loss for users, perhaps such advice should be clearly noted.
 
Old 05-13-2013, 10:48 AM   #14
anthony11
LQ Newbie
 
Registered: Jan 2012
Posts: 3

Rep: Reputation: Disabled
Yes, this is an old thread, but a timeless question.

The safer thing, and probably faster, would be to use mv at file granularity, something like

find . -type f -name \*rtn -mtime +7 -a \! -name \*archive\* -exec mv {} arc/.


(untested but you get the idea)

Using tar/cpio means you have to have room in the filesystem for two copies of the week's files at once, assuming it's one filesystem, and if it bombs in the middle you have 1.N copies where N is uncertain. The mv would be more atomic.
 
  


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
cpio -idcmv command hanged while extracting .cpio command ravindra.atre Linux - General 2 08-22-2011 05:10 AM
cannot using FTP move command to move files adrianmak Linux - Networking 4 04-21-2009 01:01 PM
how to append files using cpio? madhugp Linux - General 8 01-04-2007 09:53 AM
How to use cpio to extract a cpio archive? linx win Debian 4 06-17-2005 02:53 PM
problem of cpio -idcmv <file-name> for oracle9 downloading files richard-li Linux - General 2 02-26-2004 10:41 AM

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

All times are GMT -5. The time now is 03:31 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