如何告诉find命令转义文件名中的空格字符?

时间:2017-02-27 10:39:08

标签: bash find

我有一个单行查找命令,它以递归方式检查并打印出在特定时间范围内创建的特定文件类型的大小,所有者和名称。但是在结果中,将给出filename列,直到目录或文件名中的第一个空格字符。 有没有想过在这个单一命令中解决这个问题而不在bash中编写任何循环?谢谢!

这是命令:

find /path/to/dist -type f -iname "*.png" -newermt 2015-01-01 ! -newermt 2016-12-31 -ls | sort -k5 | sort -k5 | awk '{print $5"\t"$7"\t"$11}'

4 个答案:

答案 0 :(得分:1)

尝试将awk命令更改为:

awk '{$1=$2=$3=$4=$6=$8=$9=$10="" ; print$11}'

这样整个命令就变成了这个:

find /path/to/dist -type f -iname "*.png" -newermt 2015-01-01 ! -newermt 2016-12-31 -ls | sort -k5 | awk '{$1=$2=$3=$4=$6=$8=$9=$10="" ; print$0}'

这会在行的开头留下一些额外的空格,希望它能够满足您的需要。

我删除了sort的第二个实例,因为它对第一个实例进行排序,这似乎不太可能。

答案 1 :(得分:1)

非常感谢Arno的输入,下面这行就是完成了。我使用exec(-exec ls -lh {} \;)来使大小变得可读:

find /Path/To/Dest/ -type f -iname "*.pdf" -newermt 2015-01-01 ! -newermt 2016-12-31 -exec ls -lh {} \; |sed 's/\\ /!!!/g' | sort -k5 | awk '{gsub("!!!"," ",$11);print $3"\t"$5"\t"$9}'

答案 2 :(得分:0)

-print0的{​​{1}}行动可能就是起点。从find手册页:

find

但是 -print0 True; print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses). This allows file names that contain newlines or other types of white space to be cor‐ rectly interpreted by programs that process the find out‐ put. This option corresponds to the -0 option of xargs. 有更好的find动作:

printf

可能会完成这项工作。

答案 3 :(得分:0)

我找到了以下解决方案。您在文件名中隐藏空格。我用sed做了,我使用了一个奇怪的链条" !!!"替换" \"。然后我用awk命令替换它。这是我用于测试的命令:

find . -type f -iname "*.pdf" -newermt 2015-01-01  -ls |sed 's/\\ /!!!/g' | sort -k5 | awk '{gsub("!!!"," ",$11);print $5"\t"$7"\t"$11}'
相关问题