?
Solved

Shell Script to Decompress and Rename Files Spread Over Entire Filesystem

Posted on 2008-06-22
5
Medium Priority
?
677 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

764 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