jprx
jprx

Reputation: 31

How do I tar multiple files (which are listed in another file) to their own separate tar in a different directory?

I have the following folder structure:

parent/subdir/dir1
parent/subdir/dir2
parent/subdir/dir3
parent/subdir/dir4

I want to create the following:

parent/archive/dir1.tgz
parent/archive/dir2.tgz
parent/archive/dir3.tgz
parent/archive/dir4.tgz

The files <dir#> are listed in a file called foo.txt which resides in parent/

I want to create a shell script that will read in each dir from foo.txt, tarball them to their new location, and remove the original dir. I do not want all the dirs in foo.txt to be made into one large .tgz file. Any help?

Upvotes: 0

Views: 1184

Answers (2)

user7883492
user7883492

Reputation:

Please see if this script is what you need. This script takes two arguments. The first argument is the file containing location of directories(foo.txt) and the second argument is the location where to save tgz files(parent/archive).

#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
    cd "$2";
    echo -e "Archiving $line";
    tarName="$(basename "$line").tgz";
    tar -zcvf "$tarName" "$line";
    rm -Rf "$line";
    echo -e "Archived $line to $2/$tarName and removed $line";
done < "$1"

Upvotes: 1

Raman Sailopal
Raman Sailopal

Reputation: 12917

Use the tar -T flag to specify a file which has a list of files to create a tar archive with.

Upvotes: 0

Related Questions