Solved

Shell Script to Decompress and Rename Files Spread Over Entire Filesystem

Posted on 2008-06-22
5
671 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

707 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

18 Experts available now in Live!

Get 1:1 Help Now