unix - 仅从目录中删除文件
假设目录结构如下:
toplev/
file2.txt
file5.txt
midlev/
test.txt
anotherdirec/
other.dat
myfile.txt
furtherdown/
morefiles.txt
otherdirec/
myfile4.txt
file7.txt
如何从“anotherdirec”中删除所有文件(不是目录,也不是递归地)?在此示例中,它将删除 2 个文件(other.dat、myfile.txt)
我已在“midlev”目录中尝试了以下命令,但它给出了此错误(find: bad option -maxdepth find: [-H | -L] 路径列表谓词列表
):
find anotherdirec/ -type f -maxdepth 1
我正在运行 SunOS 5.10。
Say with a directory structure such as:
toplev/
file2.txt
file5.txt
midlev/
test.txt
anotherdirec/
other.dat
myfile.txt
furtherdown/
morefiles.txt
otherdirec/
myfile4.txt
file7.txt
How would you delete all files (not directories and not recursively) from the 'anotherdirec'? In this example it would delete 2 files (other.dat, myfile.txt)
I have tried the below command from within the 'midlev' directory but it gives this error (find: bad option -maxdepth find: [-H | -L] path-list predicate-list
):
find anotherdirec/ -type f -maxdepth 1
I'm running SunOS 5.10.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
应该为你工作。
should work for you.
Rob 的答案(rm anotherdirec/*)可能会起作用,但它有点冗长并且会生成一堆错误消息。问题是您使用的 find 版本不支持 -maxdepth 选项。如果您想避免“rm anotherdirec/*”给出的错误消息,您可以这样做:
请注意,如果任何文件包含空格或其他特殊字符,则这些解决方案都不起作用。如果这是一个问题,您可以在 $i 两边加上双引号。
Rob's answer (rm anotherdirec/*) will probably work, but it is a bit verbose and generates a bunch of error messages. The problem is that you are using a version of find that does not support the -maxdepth option. If you want to avoid the error messages that 'rm anotherdirec/*' gives, you can just do:
Note that neither of these solutions will work if any of the files contain spaces or other special characters. You can put double quotes around $i if that is an issue.
查找对选项顺序敏感。试试这个:
Find is sensitive to options order. Try this:
rm toplev/midlev/anotherdirec/*
如果您只想删除文件。rm -rf toplev/midlev/anotherdirec/*
如果要删除文件和下层目录rm toplev/midlev/anotherdirec/*
if you want to delete only files.rm -rf toplev/midlev/anotherdirec/*
if you want to delete files and lower directories