How to list the folders/files of a file.tar.gz file inside a file.tar - linux

I need to list the folder/files inside a certs.tar.gz which is inside file.tar without extracting them.
[root#git test]# tar -tf file.tar
./
./product/
./product/.git/
./product/.git/refs/
./product/.git/refs/heads/
./Release/add_or_modify.sh
./certs.tar.gz
[root#git test]#

You may want to use and condition:
tar -xf abc.tar "abc.tar.gz" && tar -ztvf abc.tar.gz
Explanation:
For listing of files we use
If file is of type tar.gz:
tar -ztvf file.tar.gz
If file is of type tar:
tar -tvf file.tar
If file is of type tar.bz2:
tar -jtvf file.tar.bz2
You can also search for files in any of the above commands. e.g:
tar -tvf file.tar.bz2 '*.txt'
For extracting files we use
tar -xf file.tar
In these commands,
t: List the contents of an archive.
v: Verbosely list files processed (display detailed information).
z: Filter the archive through gzip so that we can open compressed
(decompress) .gz tar file.
j: Filter archive through bzip2, use to decompress .bz2 files.
f filename: Use archive file called filename.
x: Extract all files from given tar, but when passed with a filename
will extract only matching files

Related

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.

Special characters in the extracted text file compressed with tar gzip

When i create the archive of a text file with this command:
tar -zcvf file.gz file.txt
and then i extract it get some strange characters in the begin and in the end of file like these:
\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000644\
How can i remove them? Is it any case to create archive of the one single text file without these special characters?
tar cfz creates a gzipped tar: file.tar.gz. It is the same as tar cf file.tar followed by gzip file.tar.
Extract such a file with tar xvfz (or gunzip file.tar.gz && tar xvf file.tar, but the z is shorter).
For single files use gzip and gunzip (possibly with -k or --keep to keep the original file).

How to create tar file with only certain extensions but omitting server generated files with similar extension?

I have a folder of files and they will be in a pattern similar to this:
original.jpg
original.200px.jpg
original.300px.jpg
original.preview.jpg
original.slider.jpg
filetwo.jpg
filetwo.200px.jpg
filetwo.300px.jpg
filetwo.preview.jpg
filetwo.slider.jpg
imagethree.jpg
imagethree.200px.jpg
imagethree.300px.jpg
imagethree.preview.jpg
imagethree.slider.jpg
I want to ONLY select the original file (original.jpg, filetwo.jpg, imagethree.jpg) and omit the server generated files. I'm trying to create a tar file of just those original files and not the dynamically generated copies.
tar -tf file.tar --wildcards '*.jpg' --exclude '*.*.jpg'
Output:
filetwo.jpg
imagethree.jpg
original.jpg
Just change -t to -x to extract instead.
To create the archive:
tar -cf file.tar *.jpg --wildcards --exclude '*.*.jpg'

Unzip a single file in a tbz archive

I have the following archived directory:
itunes20140618.tbz
I want to extract single file from it called:
itunes20140618/video
How would I do this?
So far, I am doing
$ bzip2 -d /tmp/itunes20140618.tbz
But it seems to create a tar directory of everything. How would I extract just the single video file?
There are a few different versions of tar around, but on my machine I can do this:
tar xjf archive.tbz filename
To extract filename from archive.
If that doesn't work you can use:
bzip2 -dc archive.tbz | tar xvf - filename
Which uses bzip2 to extract to stdout and then pipe to tar.
In both cases you can replace the x option with t to get a list of files. Eg:
tar tjf archive.tbz
You can use the tar command and pass the path of the desired file or folder as an argument to it:
tar xjf test.tbz /path/to/file/in/archive

Updating a single file in a compressed tar

Given a compressed archive file such as application.tar.gz which has a folder application/x/y/z.jar among others, I'd like to be able to take my most recent version of z.jar and update/refresh the archive with it.
Is there a way to do this other than something like the following?
tar -xzf application.tar.gz
cp ~/myupdatedfolder/z.jar application/x/y
tar -czf application application.tar.gz
I understand the -u switch in tar may be of use to avoid having to untar the whole thing, but I'm unsure how to use it exactly.
Well, I found the answer.
You can't use tar -u with a zipped archive. So the solution I used was the following. Note that I moved the z.jar file to a folder I created in the current directory called application/x/y for this purpose.
gzip -d application.tar.gz
tar -uf application.tar application/x/y/z.jar
gzip application.tar
When I did a tar -tf application.tar (after the update, before the gzip) it showed up properly.
If the file you want to update is text file. Then you can use vim editor directly to open the tarball that contains the file and open it, just like open folder using vim editor. Then modify the file and save it and quit.
However, if the file is a binary. I have no idea about the solution.
in my case, I had to delete the file and then add the new file with the following steps:
my tar file
file.tar
└── foo.json
└── bar.json
└── dir
└── zoo.json
and I wanted only to modify/update foo.json file without extracting and re-creating the whole tar file file.tar, Here are the commands:
tar -x -f file.tar foo.json # extract only foo.json file to my current location
# now modify the file foo.json as you want ...
tar --delete -f file.tar foo.json # delete the foo.json file from the file.tar
tar -uf file.tar foo.json # add the specific file foo.json to file.tar
compressed file:
if it is compressed file, like file.tar.gz, you will need to extract the tar file from the compressed file (in this example gzip) by using gunzip file.tar.gz which will create for you the tar file file.tar. then you will be able to do the above steps.
at the end you should compress the tar file again by using gzip file.tar which will create for you compressed file with the name file.tar.gz
sub directories:
in order to handle sub dirs you will have to keep the same structure also in the file system:
tar -x -f file.tar dir/zoo.json
# now modify the file dir/zoo.json as you want ...
tar --delete -f file.tar dir/zoo.json
tar -uf file.tar dir/zoo.json
view the file structure:
by using the less command, you can view the structure of the file:
less file.tar
drwxr-xr-x root/root 0 2020-10-18 11:43 foo.json
drwxr-xr-x root/root 0 2020-10-18 11:43 bar.json
drwxr-xr-x root/root 0 2020-10-18 11:43 dir/zoo.json

Resources