linux bash - 从另一个目录中删除一个目录中的所有文件

时间:2022-11-30 01:47:02

I want to remove all files that exist in folder new-files from another folder in linux using bash commands.

我想使用bash命令从linux中的另一个文件夹中删除文件夹new-files中存在的所有文件。

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. 我有一个文件夹,里面有一个名为new-files的文件。
  3. Now I execute cp -r new-files/* other-directory/.
  4. 现在我执行cp -r new-files / * other-directory /。
  5. 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/*.
  6. 让我们说其他目录不是我想要复制它们的目录,但它已经包含其他文件所以我不能只做rm other-directory / *。
  7. I need to delete all folders which I accidently copied. How do I do that?
  8. 我需要删除我意外复制的所有文件夹。我怎么做?

3 个解决方案

#1


14  

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.

它将列出从new-files目录复制的所有文件(不考虑new-files目录)。对于每个文件,它将删除其他目录中的复制版本。

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

但是要小心,如果新文件中的文件擦除了其他目录中的文件,则无法使用此方法恢复旧文件。您应该考虑使用版本控制系统(例如Git)。

#2


4  

From your:

从你的:

Edit, to clarify:

编辑,澄清:

  1. I got a folder with files called new-files.
  2. 我有一个文件夹,里面有一个名为new-files的文件。
  3. Now I execute cp -r new-files/* other-directory/.
  4. 现在我执行cp -r new-files / * other-directory /。
  5. 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/*.
  6. 让我们说其他目录不是我想要复制它们的目录,但它已经包含其他文件所以我不能只做rm other-directory / *。
  7. I need to delete all folders which I accidently copied. How do I do that?
  8. 我需要删除我意外复制的所有文件夹。我怎么做?

You can loop through the original dir new-files/ and delete files with same name in the other-directory/:

您可以遍历原始目录new-files /并删除other-directory /中具有相同名称的文件:

for file in /new-files/*
do
   rm /other-directory/"$file"
done

#3


2  

wee script to do what you want:

小脚做你想做的事:

pushd `pwd`
cd /path/to/new-files
x=`find . -type f`
popd

echo $x | xargs rm

#1


14  

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.

它将列出从new-files目录复制的所有文件(不考虑new-files目录)。对于每个文件,它将删除其他目录中的复制版本。

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

但是要小心,如果新文件中的文件擦除了其他目录中的文件,则无法使用此方法恢复旧文件。您应该考虑使用版本控制系统(例如Git)。

#2


4  

From your:

从你的:

Edit, to clarify:

编辑,澄清:

  1. I got a folder with files called new-files.
  2. 我有一个文件夹,里面有一个名为new-files的文件。
  3. Now I execute cp -r new-files/* other-directory/.
  4. 现在我执行cp -r new-files / * other-directory /。
  5. 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/*.
  6. 让我们说其他目录不是我想要复制它们的目录,但它已经包含其他文件所以我不能只做rm other-directory / *。
  7. I need to delete all folders which I accidently copied. How do I do that?
  8. 我需要删除我意外复制的所有文件夹。我怎么做?

You can loop through the original dir new-files/ and delete files with same name in the other-directory/:

您可以遍历原始目录new-files /并删除other-directory /中具有相同名称的文件:

for file in /new-files/*
do
   rm /other-directory/"$file"
done

#3


2  

wee script to do what you want:

小脚做你想做的事:

pushd `pwd`
cd /path/to/new-files
x=`find . -type f`
popd

echo $x | xargs rm