Gzip multiple files individually and keep the original files
Your >
in the last command gets parsed by the same shell which runs find
. Use a nested shell:
find . -type f -name "*cache.html" -exec sh -c "gzip < {} > {}.gz" \;
Compress multiple files individually with Gzip
gzip */*.txt
But the extension for each file will be .txt.gz, as gzip uses it to know the original filename.
GZip every file separately
You can use gzip *
Note:
- This will zip each file individually and DELETE the original.
- Use
-k
(--keep
) option to keep the original files. - This may not work if you have a huge number of files due to limits of the shell
- To run gzip in parallel see @MarkSetchell's answer below.
How to gzip each file separately inside a folder and then if successful delete the original file
I don't understand the issue you're having. According to the gzip man page:
Whenever possible, each file is replaced by one with the extension .gz, while keeping the same ownership modes, access and modification times.
My only guess is you're having permissions issues. Make sure you're logged in as the owner of the file that you are gzipping. However, in the gzip version on my machine, I get an obvious Operation not permitted
warning when I try to gzip a file that I don't own. The operation still succeeds, but it's clear what is going on.
How to gzip all files in all sub-directories into one compressed file in bash
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
How to gzip all files in all sub-directories in bash
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
Can multiple .gz files be combined such that they extract into a single file?
Surprisingly, this is actually possible.
The GNU zip man page states: multiple compressed files can be concatenated. In this case, gunzip will extract all members at once.
Example:
You can build the zip like this:
echo 1 > 1.txt ; echo 2 > 2.txt; echo 3 > 3.txt;
gzip 1.txt; gzip 2.txt; gzip 3.txt;
cat 1.txt.gz 2.txt.gz 3.txt.gz > all.gz
Then extract it:
gunzip -c all.gz > all.txt
The contents of all.txt
should now be:
1
2
3
Which is the same as:
cat 1.txt 2.txt 3.txt
And - as you requested - "gunzip will extract all members at once".
c# gzip - keep extension on original file but remove when compressing
If I'm understanding your question correctly, there is no solution as stated. A gzip'ed file (at least, a file gzip'ed the way you're doing it) doesn't store its name, so if you compress a file named sample.doc and the output is named sample.gz, the ".doc" part is gone forever. That's why if you compress a file with the gzip command-line utility, it the compressed version sample.doc.gz.
In some constrained situations, you might be able to guess an appropriate extension by looking at the contents of the file, but that isn't very reliable. If you just need compression, and the file format isn't constrained, you could just build a .zip file instead, which does store filenames.
Related Topics
View a Particular Line of a File Denoted by a Number
How Were the Weightings in the Linux Load Computation Chosen
Bypass Rsync Prompt "Are You Sure You Want to Continue Connecting"
Ssh: Could Not Resolve Hostname [Hostname]: Nodename Nor Servname Provided, or Not Known
One Core Exclusively for My Process
Run Bash Commands from Txt File
Performance Difference Between Ipc Shared Memory and Threads Memory
How to Delete the First Column ( Which Is in Fact Row Names) from a Data File in Linux
String Replacement in Bash - Bad Substitution Error
Easy Way of Installing Eclipse Plugins on Ubuntu
What Happens to Other Processes When a Docker Container's Pid1 Exits
Bash: Add String to the End of the File Without Line Break
How to Show Line Number When Executing Bash Script
Using Grep to Search for a String That Has a Dot in It
Using Putty to Scp from Windows to Linux