我想对文件列表重复做一些事情。问题中的文件名称中带有空格:
david@david: ls -l
total 32
-rw-rw-r-- 1 david david 13 Mai 8 11:55 haha
-rw-rw-r-- 1 david david 0 Mai 8 11:55 haha~
-rw-rw-r-- 1 david david 13 Mai 8 11:55 haha (3rd copy)
-rw-rw-r-- 1 david david 13 Mai 8 11:55 haha (4th copy)
-rw-rw-r-- 1 david david 13 Mai 8 11:55 haha (5th copy)
-rw-rw-r-- 1 david david 13 Mai 8 11:55 haha (6th copy)
-rw-rw-r-- 1 david david 13 Mai 8 11:55 haha (7th copy)
-rw-rw-r-- 1 david david 13 Mai 8 11:55 haha (another copy)
-rw-rw-r-- 1 david david 13 Mai 8 11:55 haha (copy)
现在,我要统计每个文件:
david@david: echo '
for file in $(ls)
do
stat $file
done' | bash
(我使用echo和管道来编写多行命令。)
当我这样做时,它可以正确处理名称中没有空格的那些文件。但是其他...
stat: cannot stat ‘(another’: No such file or directory
stat: cannot stat ‘copy)’: No such file or directory
更改$(ls)到"$(ls)"或$file以"$file"不起作用。我能做什么?
编辑:
echo '
for files in *
do
stat "$files"
done' | bash
绝招!由于我是bash的新手,所以我想让事情尽可能简单-设法逃脱空间,或使用xargs或解决方案read -r,尽管它们解决了问题,但没有任何帮助。
正如一些人所问:是的,使用它代替stat *是很奇怪的。但是我只是想找到一种通用的方法,使用for循环在bash中的一堆文件名上应用相同的命令。因此stat可以代表gzip,gpg或rm。
stat *?(;-)