Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to program a script that allows what is said in the title. So basically I gunzip the initrd, than unpack the cpio, open vi to allow editing, save, pack with cpio, and gzip again, so nothing fancy here (at least I hope that, I am not good at shell scripting). Now after gunzipping the archive the ending .gzip or .gz is left out so that I can't use $1 as the name. How should I delete the ending so that I can use a new variable foo, for further processing?

This is probably not an very elegant way, but I hope it works :)

# This script should make it possible to edit the preseed file
# within a initrd gzipped cpio archive, without unpacking and packing it
# manually

mkdir temporarydirectory
# $1 will be the initrd (cpio archive which is compressed with gzip)
mv $1 temporarydirectory
cd temporarydirectory
gunzip $1
cpio -id < $1 # here is where i need to cut of the gzip ending
rm $1 # again without the gzip ending cutted of
vim preseed.cfg
find . | cpio -H newc -o > $1 # again without gzip ending
gzip $1 # here the same
mv $1 .. # here the gzip ending is used again
cd ..
rm -r temporarydirectory
share|improve this question
Are you looking for parameter expansion? – jasonwryan Jan 6 '12 at 6:43
You should test the initrd using file to find the compression method. I use xz to compress initrd and gunzip wouldn't work generically. – bsd Jan 6 '12 at 11:11
Not sure why you need to extract files in initrd, most (all) distros include a variant of mkinitrd. You could just edit the file as is, and then remake the initrd – bsd Jan 6 '12 at 14:33
Why not just pipe? gunzip | cpio and later cpio | gzip. Saves you all the trouble with unnecessary temporary files. – frostschutz Jul 12 '13 at 20:27

Look at the bash Parameter Expansion docs. Removing an extension is pretty common, you can do it with:

gunzip $file
cpio -id < $cpiofile

(Replacing the positional parameters with proper variable names will make your script easier to read and maintain, especially if at some point you want to add or change the order of the parameters.)

share|improve this answer
Or with basename. basename foo.gz .gz returns foo – bsd Jan 6 '12 at 14:30

I would change the next lines

gunzip $1
cpio -id < $1 


gzip -dc $1|cpio -id


mv $1 ..


mv ${1}.gz ../$1

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.