Saving the file name when using tar - linux

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 *

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.

How to create zip archive with files with only .txt extension in Linux using terminal

I have directory "Documents" with these files:
file1.txt.
file2.txt
index.html
index.php
script.pl
I want to create zip archive named files.zip with only .txt extension files using terminal. How can I do these?
At the basic level, if you are in the same directory that your files are in, you can do :
zip files.zip *txt
And if you want to zip the files with .txt extention, by giving the absolute path, if they are in Documents directory, which will create files.zip in the current directory you are in:
zip files.zip /the/path/to/Documents/*txt
If you also want this zipped file to be in Documents folder, you should specify it as:
zip /the/path/to/Documents/files.zip /the/path/to/Documents/*txt
An add-on to the existing answer, if you are adding to an existing zip archive, be careful if you have identically named entries in the zip archive & the inpath. From man zip:
Command format. The basic command format is
zip options archive inpath inpath ...
where archive is a new or existing zip archive and inpath is a directory or file path optionally including wildcards. When given the
name of an existing zip archive, zip will replace iden‐
tically named entries in the zip archive (matching the relative names as stored in the archive) or add entries for new names. For
example, if foo.zip exists and contains foo/file1 and
foo/file2, and the directory foo contains the files foo/file1 and foo/file3, then:
zip -r foo.zip foo
or more concisely
zip -r foo foo
will replace foo/file1 in foo.zip and add foo/file3 to foo.zip. After this, foo.zip contains foo/file1, foo/file2, and foo/file3, with
foo/file2 unchanged from before.
So if before the zip command is executed foo.zip has:
foo/file1 foo/file2
and directory foo has:
file1 file3
then foo.zip will have:
foo/file1 foo/file2 foo/file3
where foo/file1 is replaced and foo/file3 is new.
Give a try to:
zip only-txt.zip `find . -name "*.txt"`
This will create a zip file named only-txt.zip including all the *.txt files located within the directory you run the command, notice that this will search for *.txt files recursively in all subfolders of the dir

Archive all the files from source directory into a xyz.gz file and move that to target directory using UNIX shell script

Requirement: Archive files using UNIX shell script into .gz format without directory structure
I am using below command
tar -C source_dir -zcvf target_dir/xyz.gz source_dir
example:
tar -C /home/log -zcvf /home/archive/xyz.gz /home/log
here xyz.gz contains /home/log
It's creating xyz.gz file maintaining the directory structure. I want only files to be archive without directory structure.
You can try the following command:
$ cd /home/log
$ tar zcvf /home/archive/xyz.gz *
You can use the --transform option to strip leading path components from the archived file names using a sed espression:
tar -C /home/log -zcvf /home/archive/xyz.gz --transform 's_.*/__' /home/log
This however will also write an entry for each encountered directory. If you don't want that, you can use find to find only regular files and pass them to tar on stdin like this:
cd /home/log
find -type f -print0 | tar -zcvf /home/archive/xyz.gz --transform 's_.*/__' --verbatim-files-from --null -T -
Note that this may create multiple entries with the same name in the tar archive, if files with the same name exist in different subdirectories. Also you should probably use the conventional .tar.gz or .tgz extension for the compressed tar archive.

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.
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

How to gzip all files in all sub-directories into one compressed file in bash

Possible Duplicate:
gzipping up a set of directories and creating a tar compressed file
This post describes how to gzip each file individually within a directory structure. However, I need to do something slightly different. I need to produce one big gzip file for all files under a certain directory. I also need to be able to specify the output filename for the compressed file (e.g., files.gz) and overwrite the old compressed file file if one already exists.
tar -zcvf compressFileName.tar.gz folderToCompress
everything in folderToCompress will go to compressFileName
Edit: After review and comments I realized that people may get confused with compressFileName without an extension. If you want you can use .tar.gz extension(as suggested) with the compressFileName
there are lots of compression methods that work recursively command line and its good to know who the end audience is.
i.e. if it is to be sent to someone running windows then zip would probably be best:
zip -r file.zip folder_to_zip
unzip filenname.zip
for other linux users or your self tar is great
tar -cvzf filename.tar.gz folder
tar -cvjf filename.tar.bz2 folder # even more compression
#change the -c to -x to above to extract
One must be careful with tar and how things are tarred up/extracted, for example if I run
cd ~
tar -cvzf passwd.tar.gz /etc/passwd
tar: Removing leading `/' from member names
/etc/passwd
pwd
/home/myusername
tar -xvzf passwd.tar.gz
this will create
/home/myusername/etc/passwd
unsure if all versions of tar do this:
Removing leading `/' from member names
#amitchhajer 's post works for GNU tar. If someone finds this post and needs it to work on a NON GNU system, they can do this:
tar cvf - folderToCompress | gzip > compressFileName
To expand the archive:
zcat compressFileName | tar xvf -

Resources