Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux bash - remove all files which are in one directory from another directory [closed]

Tags:

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:

  • I got some setup scripts which copy some pre-configured config files over. I would like to have the option to remove those files again
  • Sometimes it happens that archives get unpacked into the root of your downloads directory and not into a subdir because the person packing the file put everything to the archives root

What's the best way to do that?

Edit, to clarify:

  1. I got a folder with files called new-files.
  2. Now I execute cp -r new-files/* other-directory/.
  3. 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/*.
  4. I need to delete all folders which I accidently copied. How do I do that?
like image 339
Zulakis Avatar asked May 27 '13 12:05

Zulakis


People also ask

How do you delete all files with a specific extension in the working 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.


2 Answers

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).

like image 102
aymericbeaumet Avatar answered Oct 26 '22 09:10

aymericbeaumet


From your:

Edit, to clarify:

  1. I got a folder with files called new-files.
  2. Now I execute cp -r new-files/* other-directory/.
  3. 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/*.
  4. 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 
like image 26
fedorqui 'SO stop harming' Avatar answered Oct 26 '22 08:10

fedorqui 'SO stop harming'