简体   繁体   中英

How to delete content of file present in multiple directories in linux

Directory A having two sub directories B and C. Both B and C having same text file like "abc.txt". From directory A itself how to delete content abc.txt in both directories

If there could be more than two subdirectories in A , but you want to restrict you to B and C , you can use

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

to delete both files.

To empty their content, use

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

Delete the actual files:

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

Delete the "content" of the files:

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

使用Kleene star:

rm A/*/abc.txt

The more general way is to use find:

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

The explanation of the command is:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM