How about just this?
$ gunzip *.txt.gz
gunzip will create a gunzipped file without the .gz suffix and remove the original file by default (see below for details). *.txt.gz will be expanded by your shell to all the files matching.
This last bit can get you into trouble if it expands to a very long list of files. In that case, try using find and -exec to do the job for you.
From the man page gzip(1):
gunzip takes a list of files on its command line and replaces each file whose name ends with .gz, -gz, .z, -z, or _z (ignoring case) and which begins with the correct magic number with an uncompressed file without the original extension.
Note about 'original name'
gzip can store and restore the filename used at compression time. Even if you rename the compressed file, you can be surprised to find out it restores to the original name again.
From the gzip manpage:
By default, gzip keeps the original file name and timestamp in the compressed file. These are used when decompressing the file with the
-Noption. This is useful when the compressed file name was truncated or when the time stamp was not preserved after a file transfer.
And these file names stored in metadata can also be viewed with file:
$ echo "foo" > myfile_orig
$ gzip myfile_orig
$ mv myfile_orig.gz myfile_new.gz
$ file myfile_new.gz
myfile_new.gz: gzip compressed data, was "myfile_orig", last modified: Mon Aug 5 08:46:39 2019, from Unix
$ gunzip myfile_new.gz # gunzip without -N
$ ls myfile_*
myfile_new
$ rm myfile_*
$ echo "foo" > myfile_orig
$ gzip myfile_orig
$ mv myfile_orig.gz myfile_new.gz
# gunzip with -N
$ gunzip -N myfile_new.gz # gunzip with -N
$ ls myfile_*
myfile_orig
Answer from gertvdijk on Stack ExchangeHow about just this?
$ gunzip *.txt.gz
gunzip will create a gunzipped file without the .gz suffix and remove the original file by default (see below for details). *.txt.gz will be expanded by your shell to all the files matching.
This last bit can get you into trouble if it expands to a very long list of files. In that case, try using find and -exec to do the job for you.
From the man page gzip(1):
gunzip takes a list of files on its command line and replaces each file whose name ends with .gz, -gz, .z, -z, or _z (ignoring case) and which begins with the correct magic number with an uncompressed file without the original extension.
Note about 'original name'
gzip can store and restore the filename used at compression time. Even if you rename the compressed file, you can be surprised to find out it restores to the original name again.
From the gzip manpage:
By default, gzip keeps the original file name and timestamp in the compressed file. These are used when decompressing the file with the
-Noption. This is useful when the compressed file name was truncated or when the time stamp was not preserved after a file transfer.
And these file names stored in metadata can also be viewed with file:
$ echo "foo" > myfile_orig
$ gzip myfile_orig
$ mv myfile_orig.gz myfile_new.gz
$ file myfile_new.gz
myfile_new.gz: gzip compressed data, was "myfile_orig", last modified: Mon Aug 5 08:46:39 2019, from Unix
$ gunzip myfile_new.gz # gunzip without -N
$ ls myfile_*
myfile_new
$ rm myfile_*
$ echo "foo" > myfile_orig
$ gzip myfile_orig
$ mv myfile_orig.gz myfile_new.gz
# gunzip with -N
$ gunzip -N myfile_new.gz # gunzip with -N
$ ls myfile_*
myfile_orig
Use this command to gunzip (unzip gz files) all files in the current directory and keep the original ones:
gunzip -k *.gz
gunzip has -r option. From man gunzip :
-r --recursive
Travel the directory structure recursively. If any of the
file names specified on the command line are directories, gzip
will descend into the directory and compress all the files it finds
there (or decompress them in the case of gunzip ).
So, if you want to gunzip all compressed files (gunzip can currently decompress files created by gzip, zip, compress, compress -H or pack) inside the directory /foo/bar and all its subdirectories :
gunzip -r /foo/bar
This will handle file names with spaces too.
Using the commands below. Replace <path_of_your_zips> with the path to your ZIP files and <out> with your destination folder:
For GZ files
find <path_of_your_zips> -type f -name "*.gz" -exec tar xf {} -C <out> \;or
find <path_of_your_zips> -type f -name "*.gz" -print0 | xargs -0 -I{} tar xf {} -C <out>For ZIP files
find <path_of_your_zips> -type f -name "*.zip" -exec unzip {} -d <out> \;or
find <path_of_your_zips> -type f -name "*.zip" -print0 | xargs -0 -I{} unzip {} -d <out>
This might be what you're looking for:
for f in *.gz; do gunzip -c "$f" > /mnt/s3/data_transfer/"${f%.gz}"; done
You may remove the .gz files by rm *.gz after that, if you want.
Or, alternatively
cp *.gz /mnt/s3/data_transfer/ && cd /mnt/s3/data_transfer && gunzip *.gz
Note that the latter command will gunzip all .gz files in the directory /mnt/s3/data_transfer, including the ones that exist, if any, before the cp command is executed. If you want to remove the original .gz files, replace cp with mv.
First, let's find the files:
find . -type f -name "*.gz" > foo.sh
We find the files whose name ends with gz and save them into foo.sh. Let's open it:
vim foo.sh
Now, we prepend the gunzip command to each line. Let's hit the : key, so at the bottom of the file you can enter a command. Now, let's enter this command:
%s/^/gunzip -c /
this replaces the start of each line with the text we want to prepend.
Then we append the destination path, by pressing : again and pasting the following:
%s/$/ \/some\/path/
of course, instead of \/some\/path you will need to use your own path.
Finally, add
cd /some/path
rename 's/\.gz//' *
to remove all the gz extensions from the files in /some/path (replace this with your actual path)
Note that you may need to install rename and here I assumed that the target path already existed.
I'd prefer gzip -r ./ which does the same thing but is shorter.
No need for loops or anything more than find and gzip:
find . -type f ! -name '*.gz' -exec gzip "{}" \;
This finds all regular files in and below the current directory whose names don't end with the .gz extension (that is, all files that are not already compressed). It invokes gzip on each file individually.
Edit, based on comment from user unknown:
The curly braces ({}) are replaced with the filename, which is passed directly, as a single word, to the command following -exec as you can see here:
$ touch foo
$ touch "bar baz"
$ touch xyzzy
$ find . -exec echo {} \;
./foo
./bar baz
./xyzzy
Gzip compresses files, not directories. If you want to gzip a directory, you must first create a tarball. You can create a tarball and compress it in one step.
tar cvzf tarball.tar.gz directory/
(create, verbose, gZip, to this file)
to reverse the process
tar xvzf tarball.tar.gz
(eXtract)
tar cf - directory | gzip -9c > foo.tar.gz
Then copy this as before, then do the reverse:
tar xzf foo.tar.gz
If you're using SSH to transfer the files, you can skip the intermediate step of having a file to store and transfer:
tar cf - directory | gzip -9c | ssh user@hostname "cd /destdir ; tar xzvf -"
This is especially helpful if your resulting tar file is very large and might not fit on the destination partition along with the uncompressed data. In this case, using rsync might be better anyway.
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
gzip will always compress each file into a single .gz file when given a list of files on its command line.
For example
$ gzip -r log.2011
to recursively walk the log.2011 directory and compress all files it finds, or
$ gzip log.2011/*
to compress only the files in the log.2011 directory without descending into subdirectories.
This will output a gz archive for each matching file, files are replaced by the archive:
gzip fileprefix*
Have a look to the '-r' flag too.