www.question-defense.com | Engage: Visit :: Login :: Register
Translate to English Übersetzen Sie zum Deutsch/German Переведите к русскому/Russian Μεταφράστε στα ελληνικά/Greek Vertaal aan het Nederlands/Dutch ترجمة الى العربية/Arabic 中文翻译/Chinese Traditional 中文翻译/Chinese Simplified 한국어에게 번역하십시오/Korean 日本語に翻訳しなさい /Japanese Traduza ao Português/Portuguese Traduca ad Italiano/Italian Traduisez au Français/French Traduzca al Español/Spanish
0

I needed to clean up a directory on a server that included numerous directories each with numerous files inside each directory. I decided to write a quick shell script that would archive with tar and then compress with bzip2. The shell script requires you pass in a variable which could technically just be “*” to include all directories in the directory you are running the script from. Below is the content of the script followed by an explanation of each line of the script that will archive and compress numerous directories by looping through each directory to create a .tar.bzip2 file.

Shell Script To Archive & Compress Numerous Directories:

#!/bin/bash
echo "Archiving & Compressing All Directories"
for f in "$@"
do
tar -cjvf "new-directory/$f.tar.bz2" "$f"
rm -rf $f
done
echo "Finished with compressing directories"

Archive & Compress Script Explained:
The above script will archive, compress, and then remove any number of directories from where it is run. The first line specifies that it is a bash shell script and should be run in a bash shell regardless. The second line is simply an output line stating what is about to happen which in this case is archiving and compressing all directories specified. The third line specifies that it will do the below for each directory matching the variable specified while running the script which we will explain more below. The fourth and seventh line specify the start and end of the processing each item matching the variable in line three. The fifth line uses tar to archive and compress each directory matching $f in the “new-directory” directory. The directory named new-directory must exist before running the script so the newly processed files will be archived there. You don’t have to move them to a new directory and you can just remove “new-directory/” from the fifth line if you want the compressed files to stay in the same directory. The sixth line removes the processed directories after they are archived. The eighth line is also just a message that notifies you that the script is complete.

Run Shell Script To Process & Archive Directories:

./compress-archive *

The above shows how to run this script if it has been saved to a file by the name of compress-archive. Once you save the script make sure it is executable by the user you intend to execute it with. You can modify the permissions of the script by using the chmod command similar to the example below.

Modify Archive & Compress Script Permissions:

chmod 755 compress-archive

The “*” represents every file and directory in whatever directory you are running the script from.


Mastering Unix Shell Scripting

Randal K. Michael. Wiley 2008, Paperback, 1032 pages, $37.65

4.5


Linux Command Line and Shell Scripting Bible

Richard Blum. Wiley 2008, Paperback, 840 pages, $26.82

5.0

DeliciousStumbleUponDiggTwitterMixxTechnoratiFacebookNews VineLinkedInYahoo! Bookmarks
Related posts:
  1. Shell Script To Configure A Read Only User On A PostgreSQL Database **UPDATE: New article here. Its a little more complicated to...
  2. Use RAR On Linux To Compress A File Into Multiple Parts The rar command can be useful on Linux to not...
  3. NRPE Nagios Plugin To Check The Number Of Files In A Directory We have various scripts on a server that process files...
  4. Shell Script To Check Number Of Rows In A PostgreSQL Database The below code snipet was created to check the number...
  5. Get A List Of Files That Are Different In Multiple Directories On Windows When upgrading web applications it can make life much easier...

Tags: , , , , , , , , , , , ,
Leave a Reply