?
Solved

Shell Script to Decompress and Rename Files Spread Over Entire Filesystem

Posted on 2008-06-22
5
Medium Priority
?
683 Views
Last Modified: 2008-06-23
There are files spread over an entire filesystem compressed with bzip2.  They are all named similar to a010013l.afm.z where a010013l.afm is the proper name of the uncompressed file.

When uncompressed via gzip2 -d a010013l.afm.z (for example), the output file is a010013l.afm.z.out.  Again, the proper output name for this file is a010013l.afm

The first task of the script is, given a directory (which may or may not be /) to recursively step through all child directories looking for .z files.  It should then uncompress them, search for a .out file matching the file that was just uncompressed and rename it, removing the .z.out from the name.
0
Comment
Question by:WarriorPoet42
  • 2
  • 2
5 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 1400 total points
ID: 21842861
The reason bzip2 adds a .out extension, is that .z is not a recognised bzip2 compression extension.

Here's a script that renames before uncompression (acheives the same result)


#!/bin/bash
for file in $(find / -name "*.z")
do
  mv $file ${file%%.z}.bz2
  bzip2 -d ${file%%.z}.bz2
done

Open in new window

0
 

Author Comment

by:WarriorPoet42
ID: 21842899
It appears to be working so far.  Can you explain the syntax of the {file%%..} bit?
0
 

Author Comment

by:WarriorPoet42
ID: 21842916
Uh, found a problem.  When a directory has spaces (for instance, my Wine/Cedaga directories) I get errors like this:

mv: cannot stat `./bzips/home/nick/.cedega/coh/c_drive/Program': No such file or directory
bzip2: Can't open input file ./bzips/home/nick/.cedega/coh/c_drive/Program.bz2: No such file or directory.
mv: cannot stat `Files/City': No such file or directory
bzip2: Can't open input file Files/City.bz2: No such file or directory.
mv: cannot stat `of': No such file or directory
bzip2: Can't open input file of.bz2: No such file or directory.
mv: cannot stat `Heroes/piggs/texFxGuiItems.pigg.z': No such file or directory
bzip2: Can't open input file Heroes/piggs/texFxGuiItems.pigg.bz2: No such file or directory.


Naturally all that effort was really for one file: ./bzips/home/nick/.cedega/coh/c_drive/Program Files/City of Heroes//piggs/texFxGuiItems.pigg.z
0
 
LVL 9

Assisted Solution

by:ghostdog74
ghostdog74 earned 600 total points
ID: 21843076
do use the for loop to go through results of find or cat.
use a while loop instead. and always quote your variables

find / -name "*.z" | while read files
do
   mv $file "${file%%.z}.bz2"
  bzip2 -d "${file%%.z}.bz2"
done

Open in new window

0
 
LVL 48

Expert Comment

by:Tintin
ID: 21843276
>Can you explain the syntax of the {file%%..}

bash and ksh have various inbuilt string manipulation features.  The %% means truncate whatever string/expression to the right of %%.

So in this instance,

file.z

is renamed to

file.bz2

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question