linux bash - 从另一个目录中删除一个目录中的所有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16773457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
linux bash - remove all files which are in one directory from another directory
提问by Zulakis
I want to remove all files that exist in folder new-filesfrom another folder in linux using bash commands.
我想new-files使用 bash 命令从 linux 中的另一个文件夹中删除文件夹中存在的所有文件。
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:
编辑,澄清:
- 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?
- 我有一个文件夹,里面有一个名为
new-files. - 现在我执行
cp -r new-files/* other-directory/. - 让我们说 other-directory 不是我想将它们复制到的目录,但它已经包含其他文件,所以我不能只做 rm other-directory/*。
- 我需要删除我不小心复制的所有文件夹。我怎么做?
采纳答案by aymericbeaumet
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-filesdirectory (new-filesdirectory will not be taken in consideration). For each file, it will remove the copied version in other-directory.
它将列出从new-files目录中复制的所有文件(new-files目录不会被考虑在内)。对于每个文件,它将删除other-directory.
But you've to be careful, if a file in new-fileserase 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 Gitfor example).
但是你要小心,如果一个文件new-files删除了一个文件other-directory,你将无法使用这种方法恢复旧文件。您应该考虑使用版本控制系统(例如Git)。
回答by fedorqui 'SO stop harming'
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?
编辑,澄清:
- 我有一个文件夹,里面有一个名为
new-files.- 现在我执行
cp -r new-files/* other-directory/.- 让我们说 other-directory 不是我想将它们复制到的目录,但它已经包含其他文件,所以我不能只做 rm other-directory/*。
- 我需要删除我不小心复制的所有文件夹。我怎么做?
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
回答by wheybags
wee script to do what you want:
wee 脚本来做你想做的事:
pushd `pwd`
cd /path/to/new-files
x=`find . -type f`
popd
echo $x | xargs rm

