LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-29-2012, 02:49 PM   #1
Refractor
Member
 
Registered: Oct 2008
Location: Rousse, Bulgaria
Distribution: Debian
Posts: 91

Rep: Reputation: 25
Question Extracting the contents of a folder in a CPIO archive to another folder


Greetings,
since my English is not that good and maybe the title is a bit(lot) confusing, I'll give an example:
I have a CPIO archive with the following structure:
Code:
/folder1
|- file1
|- file2
/folder2
|-/subfolder1
|-|-file3
|-|-file4
|-/subfolder2
|-|-file5
Is there a way to extract only the contents of a folder with CPIO (either GNU or BSD) without extracting the folder itself?
when I try cpio -idv folder2/* < archive.cpio
I get a folder2 and all files in it, but what I need is the contents of folder2 without creating the parent folder itself.

I hope that cleared it. Thanks in advance!
 
Old 05-29-2012, 04:54 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
cpio cannot do that, unless you use the -r option to rename files interactively. It could be very annoying, anyway. Use pax instead that works similarly to cpio but in addition has an option to rename the archived files based on ed regexp. Here we go:

1. to list the content of the archive use pax without options
Code:
$ pax < archive.cpio
folder1
folder1/file1
folder1/file2
folder2
folder2/subfolder2
folder2/subfolder2/file5
folder2/subfolder1
folder2/subfolder1/file4
folder2/subfolder1/file3
this is just to show the archive I previously created in order to resemble your example. The output is similar to what you can obtain by running cpio -t < archive.cpo.

2. to list a partial content based on a pattern:
Code:
$ pax < archive.cpio folder2/*
folder2/subfolder2
folder2/subfolder2/file5
folder2/subfolder1
folder2/subfolder1/file4
folder2/subfolder1/file3
this is similar to cpio -t < archive.cpio folder2/*.

3. extract the content of folder2 in the current working directory (and this actually answers your question):
Code:
$ pax -r -s '/\<folder2\>\///' < archive.cpio folder2/*
$ ls
archive.cpio  subfolder1  subfolder2
$ ls -R
.:
archive.cpio  subfolder1  subfolder2

./subfolder1:
file3  file4

./subfolder2:
file5
and the trick is done!
 
1 members found this post helpful.
Old 05-30-2012, 03:32 PM   #3
Refractor
Member
 
Registered: Oct 2008
Location: Rousse, Bulgaria
Distribution: Debian
Posts: 91

Original Poster
Rep: Reputation: 25
Thanks a bunch, was wondering for ages how to get it working without scripts to move folders around!
 
Old 05-30-2012, 03:59 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You're welcome! Indeed, the -s option of pax is very powerful, since you can really extract files and sub-directories wherever you want. Just substitute the unwanted path with another and pax will create any non-existent directory tree. In addition, changing the / delimiter with another character, let you easily manage slashes in long paths, e.g.
Code:
pax -r -s ':\<folder2\>/:deep/path/where/i/want/to/extract/my/files/:' < archive.cpio folder2/*
The only caveat is the regexp that must be carefully designed to match the portion (and only that portion) of the original path we want to change or remove.
 
Old 07-10-2012, 06:12 PM   #5
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 2,557

Rep: Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763
Or you could use bsdtar, it also supports substitution with -s and can read and output in cpio format. Additionally many distros have it preinstalled as part of the libarchive package (e.g. Slackware, Arch, etc.).

Additionally bsdtar (libarchive) is more actively maintained than most of the pax implementations floating around.
 
  


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
Scripts to list folder contents and copy images from folder and subfolders brunces Linux - Newbie 6 11-03-2011 02:23 PM
A script within a folder to delete the folder, script, and the folder's contents Cyberman Programming 15 10-17-2007 08:32 AM
want to 'tar' a folder without some contents of folder shipon_97 Linux - Newbie 5 10-13-2007 05:21 AM
how do i add a folder into a rar archive steve51184 Linux - Software 6 12-06-2006 05:27 PM
Cannot see contents of my folder nr4g3d Linux - Newbie 6 02-09-2004 04:01 PM

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

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