delete files and directories in a directory according to the names of another directory

StackOverflow https://stackoverflow.com/questions/17056191

  •  31-05-2022
  •  | 
  •  

문제

This may be a simple question: I accidentally copied a whole directory (call it A) to another directory (call it B), and now I want to delete all the "directory_A files" from directory B. I realize that maybe piping is a good idea, but I don't know how to do that... can I write in command line: (assume I am currently in directory B)

ls ~/path/to/directory_A | rm -f

Thanks a lot!

도움이 되었습니까?

해결책

You were very close. Assuming current working directory is directory_B:

ls ~/path/to/directory_A | xargs rm

다른 팁

cd directoryA
ls directoryA > /tmp/t.lis
cd directoryB
while read fname
do
   rm "$fname"
done</tmp/t.lis

One approach to this.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top