如何删除linux中多个目录中存在的文件内容

时间:2016-01-21 14:30:40

标签: linux

目录A有两个子目录B和C.B和C都有相同的文本文件,如“abc.txt”。从目录A本身如何删除两个目录中的内容abc.txt

5 个答案:

答案 0 :(得分:3)

如果A中有两个以上的子目录,但是你 想要限制你BC,你可以使用

rm A/{B,C}/abc.txt

删除这两个文件。

要清空其内容,请使用

: > A/{B,C}/abc.txt

答案 1 :(得分:2)

删除实际文件:

find A/ -name "abc.txt" -delete

删除"内容"的文件:

find A/ -name "abc.txt" -exec truncate -s 0 {} \;

答案 2 :(得分:1)

使用Kleene明星:

rm A/*/abc.txt

答案 3 :(得分:1)

更通用的方法是使用find:

find . -type f -name "file" -exec rm -f {} \;

该命令的解释是:

-name "file" : file name.
-exec rm -f {} \; : delete the files that match.
-type f : specify the type of file, directory are excluded.

答案 4 :(得分:1)

找到A / -name" abc.txt" -type f -exec rm -rf {} \;