[英]Create tar gz on linux with specific list of files from sed output
Here is my command line: 这是我的命令行:
find . -type f -exec file {} \; \
| sed 's/\(.*png\): .* \([0-9]* x [0-9]*\).*/\2 \1/' \
| sed 's/\(.*jpg\): .* \([0-9]*x[0-9]*\).*/\2 \1/' \
| awk 'int($1) < 1000' \
| sed 's/^.*[[:blank:]]//' \
| tar -czvf images.tar.gz --null -T -
And the error i got is: 我得到的错误是:
tar: Unix\\n./test.png\\n./test2.jpg\\n: Cannot stat: No such file or directory
tar:Unix \\ n./test.png \\ n./test2.jpg \\ n:无法统计:没有此类文件或目录
tar: Exiting with failure status due to previous errorstar:由于先前的错误而以失败状态退出
What i want is to find all images in current directory, who's width less than 1000 px and tar them into archive. 我想要的是在当前目录中查找宽度小于1000像素的所有图像,然后将它们压缩到存档中。
to use --null
, you need to convert newlines to nulls first: 要使用
--null
,您需要先将换行符转换为null:
...
| tr '\n' '\0' \
| tar -czvf images.tar.gz --null -T -
(tested, working.) (经过测试,正常工作。)
also, here are a number of suggestions on speed and style in decreasing order of importance. 同样,这里有一些关于速度和样式的建议,以重要性从小到大的顺序排列。
a. 一种。 don't find and run
file
on more files than you need to: 不要在超出所需数量的
file
上查找和运行file
:
find . -type f -iname "*.png" -or -iname "*.jpg"
b. b。 for commands that can run on multiple files per command, such as
file
, use xargs
to save a lot of time: 对于每个命令可以在多个文件上运行的命令(例如
file
,请使用xargs
节省大量时间:
find . -type f -iname "*.png" -or -iname "*.jpg" -print0 | xargs -0 file
c. C。 if you put
|
如果放
|
at the end of each line, you can continue on the next line without also using \\
. 在每一行的末尾,您可以不使用
\\
继续下一行。
find . -type f -iname "*.png" -or -iname "*.jpg" -print0 |
xargs -0 file
d. d。 you can save yourself a lot of trouble since your max width is 999 by just
grep
ing for 1, 2, or 3 digit widths, though the awk '$1<1000'
is ultimately better in case you ever want to use a different threshold: 你可以保存自己,因为你的最大宽度很大的麻烦是999只
grep
荷兰国际集团的1,2,或3位的宽度,虽然awk '$1<1000'
是的情况下,最终更好地你想使用不同的阈值:
find . -type f -iname "*.png" -or -iname "*.jpg" -print0 |
xargs -0 file |
grep ', [0-9][0-9]\?[0-9]\? x '
e. e。
grep
and awk
are faster than sed
, so use them where possible: grep
和awk
比sed
快,因此请尽可能使用它们:
find . -type f -iname "*.png" -or -iname "*.jpg" -print0 |
xargs -0 file |
grep ', [0-9][0-9]\?[0-9]\? x ' |
grep -o -i '.*\.\(png\|jpg\)'
final command: 最终命令:
find . -type f -iname "*.png" -or -iname "*.jpg" -print0 |
xargs -0 file |
grep ', [0-9][0-9]\?[0-9]\? x ' |
grep -o -i '.*\.\(png\|jpg\)' |
tr '\n' '\0' |
tar -czvf images.tar.gz --null -T -
You can also use awk
only with : 您还可以仅将
awk
与结合使用:
find . -type f \( -name "*.png" -or -name "*.jpg" \) -exec file {} \; | awk -v width_limit=1000 '
{
match($0, /,\s+([0-9]+)\s*x\s*([0-9]+)/, items)
if (items[1] < width_limit){
match($0, /(.*):/, filename)
print filename[1]
}
}' | tar -czvf allfiles.tar -T -
The width can be configured with width_limit
variable 可以使用
width_limit
变量配置宽度
perl
: perl
快速方法: find . -type f -exec file {} + |
perl -ne '
print $1."\0" if /^(.*):\s*(JPEG|PNG).*,\s*(\d+)\s+x\s*\d+\s*,/ &&
$3 < 1000;
' | tar -czvf images.tar.gz --null -T -
Using +
operator to find
as same effect than print0 | xargs -0
使用
+
运算符find
与print0 | xargs -0
相同的效果print0 | xargs -0
print0 | xargs -0
. print0 | xargs -0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.