Shell Script To Compress Many Directories Into Many Separate tar.bzip2 Files
Posted by alex in Insights at 1:46 PMI 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.
- Shell Script To Configure A Read Only User On A PostgreSQL Database **UPDATE: New article here. Its a little more complicated to...
- Use RAR On Linux To Compress A File Into Multiple Parts The rar command can be useful on Linux to not...
- NRPE Nagios Plugin To Check The Number Of Files In A Directory We have various scripts on a server that process files...
- Shell Script To Check Number Of Rows In A PostgreSQL Database The below code snipet was created to check the number...
- Get A List Of Files That Are Different In Multiple Directories On Windows When upgrading web applications it can make life much easier...
Tags: archive, bash, bzip2, chmod, compress, do, done, echo, for, rm, script, shell, tar


























Entries (RSS)