unix - 仅从目录中删除文件

发布于 2024-12-24 19:46:18 字数 715 浏览 3 评论 0原文

假设目录结构如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沧桑㈠ 2024-12-31 19:46:18
rm anotherdirec/*

应该为你工作。

rm anotherdirec/*

should work for you.

∞觅青森が 2024-12-31 19:46:18

Rob 的答案(rm anotherdirec/*)可能会起作用,但它有点冗长并且会生成一堆错误消息。问题是您使用的 find 版本不支持 -maxdepth 选项。如果您想避免“rm anotherdirec/*”给出的错误消息,您可以这样做:

for i in anotherdirec/*; do test -f $i && rm $i; done

请注意,如果任何文件包含空格或其他特殊字符,则这些解决方案都不起作用。如果这是一个问题,您可以在 $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:

for i in anotherdirec/*; do test -f $i && rm $i; done

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.

橪书 2024-12-31 19:46:18

查找对选项顺序敏感。试试这个:

find anotherdirec/ -maxdepth 1 -type f -exec rm {} \;

Find is sensitive to options order. Try this:

find anotherdirec/ -maxdepth 1 -type f -exec rm {} \;
隔岸观火 2024-12-31 19:46:18

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文