Solved

Shell Script to Decompress and Rename Files Spread Over Entire Filesystem

Posted on 2008-06-22
5
672 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 350 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 150 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now