Create tar with same name as file but in different folder - linux

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.

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

Related

Zip and Tar compress entire top directory and not (sub)directory

I'm trying to only zip the directory where I am exactly, this is part of a bigger bash script so I need to cd into the directory where I want to extract files and then exit.
However, using either tar or zip, the entire top directory path is recreated and not just the subdirectory that I'm interested in.
I get the following error:
zip warning: name not matched: $PWD/*
What I'm trying to do:
#Sub Directory and contents will be compressed
cd "$Sub_Dir"
Zipped_Files=$(basename "$Sub_Dir")
zip -r "$Zipped_Files".zip "$PWD/*"
#or
zip -j "$Zipped_Files".zip "$PWD/*"
#or
#tar -zcf "$Zipped_Files".zip "$PWD"
echo "Files have been compressed"
You have already cd into the sub dir, zip -r "$Zipped_Files".zip ./* should work.

Saving the file name when using tar

I'm trying to create archive file. the folder contains files with Russian names. these names are not saved correctly in the archive. Is there a way to save these names in Russian using "tar" or some other command?
myDate=$(date +"%Y%m%d")
myTime=$(date +"%H%M%S")
cd /data/folder1
tar -czvf /data/folder2/folder1_$myDate'_'$myTime.tar.gz *

Staying in another folder, how can i tar specific files from another directory?

Thanks for your support,
I have the following folder structure on my linux laptop
/home
/A
/B
In folder "B", I have files of type *.csv, *.dat.
Now from folder A, How can I create a tar file containing files *.csv in folder B. I am running the command in folder A
Here is the command, I have tried but its not working,
In /home/A folder, I am running the following command
tar -cf /home/A/Sample1.tar -C /home/B/ZSBSDP4 *.csv
and also tried with this,
tar -cf /home/A/Sample1.tar -C /home/B/ZSBSDP4 --wildcards *.csv
For both of the commands, I get the following error,
tar: *.csv: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
In the tar file, I dont want to include the whole folder structure and this is the reason, I am using option -C (capital)
Moreover, the following command works but it tars all *.csv and *.dat files.
tar -cf /home/A/Sample1.tar -C /home/B/ZSBSDP4 .
You can edit the names in the tar command to remove the path. (Assuming that you have GNU tar.)
tar -cf /home/A/Sample1.tar --transform 's,.*/\([^/]*\),\1,' /home/B/ZSBSDP4/*.csv
Note that if you specify more source directories on the command, you could accidentally put more than one file with the same name in the tar file. Then when unpacking, the last one will overwrite those with the same name that precede it.
You can use the --exclude=PATTERN option:
tar -cf /home/A/Sample1.tar -C /home/B/ZSBSDP4 . --exclude=*.dat
Other "local file selection" options listed in the man page: http://linux.die.net/man/1/tar

Using Cygwin to zip individual files in a folder and zipping the entire folder

i am trying to zip individual files in a folder. i can zip the directory using
tar -zcf <filename> <location to place the file after zipping>
please help...
I do it like this
tar acf foo.tar.gz README.txt bar.txt dog.txt
ref

how to use relative path in .sh in order to make my tar files include only the folder tar is created from instead of its fullpath

In a mono exe I am creating .sh scripts file containing codes as below
tarcreator.sh:
tar -cvf /data/folder1/folder2/.../xyz.tar /data/...../tarHomeFolder/myTarFolder > /data/logs/tarLogs.txt
....
and the codes continue.
this .sh file is located in /data/...../tarHomeFolder and in my .exe I call it using "bash 'full path of .sh file'"
When I run this code in this way, it creates the tar but when I open the tar I see folders from the root directory.. (data/..../tarHomeFolder/myTarFolder/.....) instead of this I want my tar contain only directory structure of its folder (myTarFolder).
Then I changed the code to :
tar -cvf /data/folder1/folder2/.../xyz.tar myTarFolder > /data/logs/tarLogs.txt
knowing that .sh file is in tarHomeFolder but then my script tells no such file or directory for myTarFolder ..
when I use tar command right in command prompt, using the command
tar -cvf /data/folder1/folder2/.../xyz.tar myTarFolder > /data/logs/tarLogs.txt
it creates the tar directory structure as I want, so, how can I make it do the same in my bash file created and executed by mono .exe ?
All you need to do is change your directory before running tar. Something like this:
# If your full path to the directory is in DIR = "/data/...../tarHomeFolder/myTarFolder"
cd $(dirname $DIR)
tar -cvf /data/folder1/folder2/.../xyz.tar $(basename $DIR) > /data/logs/tarLogs.txt
This will leave you with relative paths, starting with myTarFolder, in your tar file.
Useful references:
basename
dirname
Something like this:
tar -cvf /data/folder1/folder2/.../xyz.tar -C /data/...../tarHomeFolder/ myTarFolder > /data/logs/tarLogs.txt

Resources