Compress Each Folder With One Archive if Archive Is Not Exist

The provided command is a shell script written in Bash that iterates through directories in the current directory, checks if an archive file (.tar.gz) already exists for each directory, and if not, it creates a compressed archive of the directory using the tar command.

Here’s an explanation of what the script does:

1
2
3
4
5
for dir in `find . -maxdepth 1 -type d  | grep -v "^\.$" `; do
    if ! [ -e ${dir}.tar.gz ] ; then
        tar -cvzf ${dir}.tar.gz ${dir}
    fi
done
  1. for dir in find . -maxdepth 1 -type d | grep -v “^.$” ; do: This line starts a loop that iterates through all subdirectories (excluding the current directory) in the current directory.

  2. if ! [ -e ${dir}.tar.gz ] ; then: This line checks if an archive file with the name ${dir}.tar.gz does not exist in the current directory.

  3. Inside the if block, tar -cvzf ${dir}.tar.gz ${dir} creates a compressed archive of the current directory (${dir}) with the name ${dir}.tar.gz. Here’s what the options used with the tar command do:

    • -c: Create a new archive.
    • -v: Verbosely list the files processed.
    • -z: Compress the archive using gzip.
    • -f: Specifies the archive file’s name.
  4. The done keyword marks the end of the loop.

So, this script essentially compresses each subdirectory in the current directory into a .tar.gz archive file if such an archive does not already exist for that subdirectory.

If you have any specific questions or need further assistance with this script, please let me know.

0%