Aniket
Aniket

Reputation: 29

Create tar with same name as file but in different folder

I have multiple files in a folder. Is there any way to create individual tar file from each of these file with same name as file and save those tar files in different folder.

e.g suppose I have files 'file1', 'file2', 'file3' in a folder then I want to create tar files named 'file1.tar.gz', 'file2.tar.gz', 'file3.tar.gz' in some different folder.

Upvotes: 0

Views: 6246

Answers (1)

mittelmania
mittelmania

Reputation: 3561

Just run this simple shell script from inside your directory:

for file in *; do tar -czf $file.tar.gz $file; done

The result will be a new .tar.gz file for each one of the files inside the directory you're in. If you wish to create the tar archives in another directory, just change the path that's right after the -cf flags. For example, if you wish to create the archives in another directory that's located in the same directory as your current one, write:

for file in *; do tar -czf ../<Other_Directory>/$file.tar.gz $file; done

Upvotes: 3

Related Questions