I want to compress files individually with the particular pattern. For example, I have directory as below :
/log/log1
-/1/log2/file.1
-/1/log2/file123
-/1/log2/file2.1
/log/log2
-/2/log2/file4.1
-/2/log2/file345
-/2/log2/file3.1
I want to compress all the files having extension .1 inside /log recursively.
The result will look like
/log/log1
-/1/log2/file.gz
-/1/log2/file123
-/1/log2/file2.gz
/log/log2
-/2/log2/file4.gz
-/2/log2/file345
-/2/log2/file3.gz
find
+ bash
solution:
find /log -type f -name "*.1" -exec bash -c 'gzip -nc "$1" > "${1:0:-2}.gz"; rm "$1"' _ {} \;
gzip
options:
-n
- when compressing, do not save the original file name and time stamp by default-c
- write output on standard output; keep original files unchanged${1:0:-2}
- bash
's slicing; get filepath with the last 2 characters truncated
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加