I want to remove all files that exist in folder new-files from another folder in linux using bash commands.
I need this for two things:
What's the best way to do that?
Edit, to clarify:
new-files.cp -r new-files/* other-directory/. Using rm Command To remove a file with a particular extension, use the command 'rm'. This command is very easy to use, and its syntax is something like this.
You could use the following command:
cd new-files ; find . -exec rm -rf path/to/other-directory/{} \; It will list all the files that where copied from the new-files directory (new-files directory will not be taken in consideration). For each file, it will remove the copied version in other-directory.
But you've to be careful, if a file in new-files erase a file in other-directory, you won't be able to restore the old file using this method. You should consider to use a versioning system (like Git for example).
From your:
Edit, to clarify:
- I got a folder with files called
new-files.- Now I execute
cp -r new-files/* other-directory/.- Lets say other-directory is not the directory I wanted to copy them to but it already contains other files so I can't just do rm other-directory/*.
- I need to delete all folders which I accidently copied. How do I do that?
You can loop through the original dir new-files/ and delete files with same name in the other-directory/:
for file in /new-files/* do rm /other-directory/"$file" done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With