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

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

  •  31-05-2022
  •  | 
  •  

Question

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!

Was it helpful?

Solution

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

ls ~/path/to/directory_A | xargs rm

OTHER TIPS

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

One approach to this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top