[英]OS X: list and remove files with spaces
I'd like to do this in OS X: 我想在OS X中这样做:
ls -rt | xargs rm -i
However, rm
is choking on the fact that some of the files have whitespaces. 然而,
rm
对某些文件有空格这一事实感到窒息。
I mention OS X because BSD's version of ls
does not have a -Q
flag. 我提到OS X因为BSD的
ls
版本没有-Q
标志。
Is there a way to do this without having to use find -print0
? 有没有办法在不使用
find -print0
情况下执行此操作?
[sgeorge@sgeorge-ld staCK]$ touch "file name"{1..5}.txt
[sgeorge@sgeorge-ld staCK]$ ls -1
file name1.txt
file name2.txt
file name3.txt
file name4.txt
file name5.txt
[sgeorge@sgeorge-ld staCK]$ ls -rt | xargs -I{} rm -v {}
removed `file name5.txt'
removed `file name4.txt'
removed `file name3.txt'
removed `file name2.txt'
removed `file name1.txt'
OR 要么
[sgeorge@sgeorge-ld staCK]$ ls -1
file a
file b
file c
file d
[sgeorge@sgeorge-ld staCK]$ OLDIFS=$IFS; IFS=$'\n'; for i in `ls -1` ; do rm -i $i ; done ; IFS=$OLDIFS
rm: remove regular empty file `file a'? y
rm: remove regular empty file `file b'? y
rm: remove regular empty file `file c'? y
rm: remove regular empty file `file d'? y
Just have find delete it for you. 只是找到你删除它。
find . -print -depth 1 -exec rm -i {} \;
It's more flexible, should be ok with spaces. 它更灵活,应该可以使用空格。
If you want to delete all files, what's wrong with rm -i *
? 如果你想删除所有文件,
rm -i *
什么问题?
You have two options. 你有两个选择。 You can either call xargs with the -0 option, which splits the input into arguments using NUL characters (
\\0
) as delimiters: 您可以使用-0选项调用xargs,该选项使用NUL字符(
\\0
)作为分隔符将输入拆分为参数:
ls -rt | tr '\n' '\0' | xargs -0 rm -i
or you can use the -I option to split the input on newlines only ( \\n
) and call the desired command once for each line of the input: 或者您可以使用-I选项仅在换行符(
\\n
)上拆分输入,并为输入的每一行调用一次所需的命令:
ls -rt | xargs -I_ rm -i _
The difference is that the first version only calls rm
once, with all the arguments provided as a single list, while the second one calls rm
individually for each line in the input. 区别在于第一个版本只调用
rm
一次,所有参数作为单个列表提供,而第二个版本分别为输入中的每一行调用rm
。
Try this 尝试这个
while read -u 3
do
rm -i "$REPLY"
done 3< <(ls -rt)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.