Zipping 2 files into a single folder in unix - linux

My requirement is to send attachment in mail from unix.
There are many attachments, So I need to zip all files into a single folder.
So far I tried:
gzip -c abc.txt > xyz.gz
gzip -c cde.txt >> xyz.gz
But it is behaving like this:
cat abc.txt cde.txt

You can use the below command.
gzip -c abc.txt cde.txt > xyz.gz
If you want more information about the gzip see the man page for the gzip.

The common way to do this would be to bundle the files into a .tar file, then gzip it. This can be done by the tar command alone:
tar -cvzf xyz.tar.gz abc.txt cde.txt
The -z flag tells tar to gzip it's output.

Related

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

gzip on a folder without changing the file extension

So I am using amazon to serve a few files.
I want to gzip these files before I upload them
first I copy templates into a new folder
cp -r templates/ templatesGZIP
then I GZIP that folder
gzip -r templatesGZIP
the problem is that this adds .gz to all the file names. so for example homeTemplate.html changes to homeTemplate.html.gz
is there any way when running gzip -r templatesGZIP that I state I want to keep the extensions the same
Thanks
Dan
Bash script: Gzip an entire folder and keep files extensions same
This would surely help first compress them with gzip and then rename them.
Thanks & Regards,
Alok
gzip just does one thing, turns a single file into a gz archive. What you need is a tar.gz file. Your friend is tar, which can use gzip as well
cp -r templates templatesGZIP
tar czf templatesGZIP.tar.gz templatesGZIP
Backround: tar does another one thing well: it turns a directory structure into a single file. The tar commands above, explained:
c = create
z = zipped, default gzip
f FILE = name of the archive file
after copying the directory
find templatesGZIP -type f ! -wholename *images* -exec gzip {} \; -exec mv {}.gz {} \;
You can use the stdout as a temporary buffer and write the output in a file of your choise:
gzip -cr templatesGZIP > outputfile.extension
There is no option in gzip to do this. Using the two-directory method, the following should do the trick
for in_file in $(find templates -type f)
do
out_file="templatesGZIP/${in_file#templates/}"
mkdir -p "$(dirname "$out_file")"
gzip -c <"$in_file" >"$out_file"
done
With this method, there's no need for the cp command.
This has been tested.
One comment on this - it's uncommon to see gzip'ed files without an extension.

tar/gzip excluding certain files

I have a directory with many sub-directories. In some of those sub-directories I have files with *.asc extension and some with *.xdr.
I want to create a SINGLE tarball/gzip file which maintains the directory structure but excludes all files with the *.xdr extension.
How can I do this?
I did something like find . -depth -name *.asc -exec gzip -r9 {} + but this gzips every *.asc file individually which is not what I want to do.
You need to use the --exclude option:
tar -zc -f test.tar.gz --exclude='*.xdr' *
gzip will always handle files individually. If you want a bundled archive you will have to tar the files first and then gzip the result, hence you will end up with a .tar.gz file or .tgz for short.
To get a better control over what you are doing, you can first find the files using the command you already posted (with -print instead of the gzip command) and put them into a file, then use this file (=filelist.txt) to instruct tar with what to archive
tar -T filelist.txt -c -v -f myarchive.tar

How to download a file in a shell script after connecting to another server via ssh?

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.

GZip on Linux to archive files specified in the text file

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`

Resources