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).
Related
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
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.
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
My goal:
having a shellscript for a cronjob (on MacOSX Snow Leopard) that connects to a Debian machine with ssh (public/private key login), executes a tar command and downloads the tarred file afterwards.
My problem:
The login works, also the execution of some commands. But how can I download a file back to the local machine?
This is what I have so far:
This is the content of the shell script so far:
#!/bin/bash
ssh user#remotehost << 'ENDSSH'
tar -C / -czf /home/user/stuff.tar.gz /home/user/stuff
ENDSSH
Short and simple, no heredoc needed.
ssh -Te none user#remotehost "tar -C / -cz /home/user/stuff" >stuff.tar.gz
Stream it back.
#!/bin/bash
ssh user#remotehost << 'ENDSSH' > stuff.tar.gz
tar -C / -czf - /home/user/stuff
ENDSSH
this might be want you want.
scp stuff.tar.gz user#remotehost:/"directory to place this file"/
Simply rsync the file once it's created:
#!/bin/bash
ssh user#remotehost tar -C / -czf /home/user/stuff.tar.gz /home/user/stuff
rsync -chavP --stats user#remotehost:/home/user/stuff.tar.gz .
This does initiate a second connection to remotehost but will save you copying data across the network when the file has not changed (much) since the last time it was archived.
Why downloading the tar file and not create the tar content on stdout?
Ie:
ssh user#machine '(' cd /the/dir '&&' tar cf - list of files ')' >archive.tar
I. How to compress files or folders via SSH
For different compressed formats, you need to use different command lines:
Zip
To compress a file or folder to a zip file:
zip -r file.zip file
Bz2
To compress a file (ONLY) to a bz2 file:
Bzip2 -zk file
Gz
To compress a file (ONLY) to a gz file:
gzip -c file > file.gz
By the way, you need to change the above "file" to the file name with extension (if any) you want to compress, while you can replace the following "xxx" with any keywords:
Tar
To compress one file or folder to a tar file:
tar -cvf xxx.tar file
To compress multiple files and/or folders to a tar file:
tar -cvf xxx.tar file1 file2 folder1 folder2 ...
Tar.bz2
To compress one file or folder to a tar.bz2 file:
tar -cvjf xxx.tar.bz2 file
To compress multiple files and/or folders to a tar.bz2 file:
tar -cvjf xxx.tar.bz2 file1 file2 folder1 folder2 ...
Tar.gz
To compress one file or folder to a tar.gz file:
tar -cvzf xxx.tar.gz file
To compress multiple files and/or folders to a tar.gz file:
tar -cvzf xxx.tar.gz file1 file2 folder1 folder2 ...
II. How to extract file via SSH
To extract a file will be easier, since you don't need to worry about folders:
Zip
To extract a zip file:
unzip file.zip
Bz2
To extract a bz2 file:
bunzip2 file.bz2
Gz
To extract a gz file:
gzip -d file.gz
Tar
To extract a tar file:
tar -xvf file.tar
Tar.bz2
To extract a tar.bz2 file:
tar -xvjf file.tar.bz2
Tar.gz
To extract a tar.gz file:
tar -xvzf file.tar.gz
By the way, you need to replace the above "file"s of the compressed files with the real file names.
Bonus:
Besides remote servers, the above command lines are also available for a Mac OS computer with the Terminal application.
I have a text file with paths to the list of files I want to compress into a singe archive. How can I pass this file to GZIP so it can create that archive with all files specified in the list?
Milan
gzip can only handle a single file at a time. You'll need to archive the files using tar first. Tar can do the compression at the same time (using the "z" argument).
tar cfz archive.tar.gz `cat file`
Well, in the first place, gzip doesn't compress multiple files into a single one, so you'll first tar. At least the GNU tar I checked has the option
-T, --files-from F
get names to extract or create from file F
so I suppose tar cfzvT target.tar.gz sourcelist would work.
gzip only compresses a single file. Use:
tar czf target.tar.gz `cat listoffile`