Solved

Shell Script to Decompress and Rename Files Spread Over Entire Filesystem

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

696 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