LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-20-2011, 04:51 PM   #1
sonichedgehog
Member
 
Registered: Oct 2007
Location: London UK
Distribution: Fedora Core 17
Posts: 298

Rep: Reputation: 32
CPIO archive with absolute paths- extraction to a different directory?


Hi
I have several computers with the relevant part of the file tree identical- so I can work at any of them and extract the files I've just modified or created, either to removable drive or to remote server. I then update the "master" computer when convenient. I use cpio for this.
Occasionally, I want to take a good look at some of the files before copying them to the "master". This has been tricky- in the end, I found that peazip allows extraction of cpio archives (etc) to any specified path.
I am aware of cpio creation with relative path names- maybe this will allow greater flexibility when restoring, but might be less successful at keeping the various servers identical. (I want to avoid having servers running needlessly, so haven't used the various sync options.)
Is there a commandline method of extracting absolute path cpio archive to a "safe" directory for viewing? Or should I continue to use peazip? -thanks
 
Old 11-20-2011, 06:36 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Create a temporary directory for the cpio archive into, say
Code:
sudo mkdir /tmp/cpio-root
To extract the contents under that directory, use
Code:
sudo sh -c 'cd /tmp/cpio-root && cpio -i --no-absolute-filenames' < /path/to/cpio/archive
or, if it is a gzipped cpio archive, use
Code:
zcat /path/to/cpio/archive | sudo sh -c 'cd /tmp/cpio-root && cpio -i --no-absolute-filenames'
The --no-absolute-filenames makes sure the contents are extracted under the current directory (i.e. using relative paths).

After you're done, just remove the temporary directory:
Code:
sudo rmdir -rf /tmp/cpio-root
The sudo above is only needed if it is important to keep the file and directory ownership as defined in the archive. If you are only interested in the file contents, you can drop the sudo from all of the above commands. For example, you can examine initial ramdisks (/boot/initrd*) as a normal user. (Just remember not to pack it back into a new cpio archive, because the user, and not root, will own all files and directories.)
 
1 members found this post helpful.
Old 11-23-2011, 08:10 AM   #3
sonichedgehog
Member
 
Registered: Oct 2007
Location: London UK
Distribution: Fedora Core 17
Posts: 298

Original Poster
Rep: Reputation: 32
Thank you, that was exactly what I wanted... I'm a little surprised that I couldn't find the --no-absolute-filenames argument on my own. It's also made me think about the use of sh -c with && - I'm assuming that && is is used either within a script or a alias and therefore requires sh -c?
 
Old 11-23-2011, 09:02 AM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by sonichedgehog View Post
It's also made me think about the use of sh -c with && - I'm assuming that && is is used either within a script or a alias and therefore requires sh -c?
In POSIX and Bourne shells, && is an operator that can be read as "and if successful, then".

The idea behind
Code:
sudo sh -c 'cd /some/directory && some-command ...'
is that the entire sh scriptlet (the part in single quotes) is run as root, and that some-command ... is run only if the cd command succeeds. If /some/directory cannot be entered, some-command ... will not be run.

Because current working directory is a per-process concept, using
Code:
cd /some/directory && sudo some-command ...
is not the same thing; it changes the working directory in the original shell first. After the command is run, the working directory has changed to /some/directory in the original shell.

Using the sudo sh -c 'cd ... && command' construct, the working directory is only changed in the subshell running as root, not in the original shell. During and after the command is run, the working directory for the original shell does not change at all.

(Using sudo cd /some/directory && some-command ... would change the directory as root in a sub-shell, but the sub-shell would immediately exit. The some-command ... would be run in the original shell, and not with root rights.)

In other words, because I have a command or commands I'd like to run, as root, using a POSIX or Bourne shell, I use sudo sh . The -c '...' is actually a parameter to sh, specifying the commands it should run.
 
  


Reply



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
Help with cp using absolute paths and globbing. Dru-Jitsu Linux - Newbie 1 06-24-2010 11:23 AM
Shell Script how to remove absolute paths from zip archive hi_irf Linux - Newbie 1 10-03-2009 05:59 AM
[SOLVED] untar backup into new directory? absolute paths in tar qwertyjjj Linux - Newbie 4 08-19-2009 03:41 PM
how to copy-in cpio archive to temp directory jxi Slackware 2 03-08-2006 04:32 PM
How to use cpio to extract a cpio archive? linx win Debian 4 06-17-2005 01:53 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:08 PM.

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
Open Source Consulting | Domain Registration