运维开发网

Ubuntu下删除两个文件夹下相同文件名且相同内容的文件V1.1(分色排版)

运维开发网 https://www.qedev.com 2020-03-19 12:08 出处:网络 作者:运维开发网整理
Ubuntu下删除两个文件夹下相同文件名且相同内容的文件 2017/5/2 17:09    

Ubuntu下删除两个文件夹下相同文件名且相同内容的文件

2017/5/2 17:09

 

 

缘起:一直在维护android的BSP,有时候发现另外一个来源的BSP有你所需要的一些功能。

因此需要使用一个工具,比对两份代码,将两份BSP里面的相同文件名(内容也完全相同)的文件删除掉。

 

 

1、列出两个文件夹dir1、dir2下相同的文件:

(英文版本ubuntu)

diff -rs dir1 dir2 | grep 'identical$'  

 

(中文版本ubuntu)

rootroot@cm-System-Product-Name:/home/wwt$ diff -rs prk_tina_bak/ fds_tinav2.1/ | grep "相同$" > 3.txt

 

 

比较之前预先删除.git和.repo文件夹。

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$ rm .repo/ -rf

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$ cd ../prk_tina_bak/

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$ rm .repo/ -rf

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$

 

 

rootroot@cm-System-Product-Name:/home/wwt$ cd prk_tina_bak/

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$ find . -name .git

./target/.git

./dl/.git

./scripts/.git

./toolchain/.git

./lichee/brandy/.git

./lichee/Linux-3.4/.git

./.repo/manifests/.git

./.repo/repo/.git

./tools/.git

./package/.git

./config/.git

./prebuilt/.git

./docs/.git

./build/.git

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$ find . -name .git | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$ find . -name .git

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$ cd ../fds_tinav2.1/

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$ find . -name .git

./target/.git

./dl/.git

./scripts/.git

./toolchain/.git

./lichee/brandy/.git

./lichee/Linux-3.4/.git

./.repo/manifests/.git

./.repo/repo/.git

./tools/.git

./package/.git

./config/.git

./prebuilt/.git

./docs/.git

./build/.git

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$ find . -name .git | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$ find . -name .git

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$

 

 

 

 

正式使用diff比对,将不同的部分存储到文件:3.txt

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$ cd ..

rootroot@cm-System-Product-Name:/home/wwt$ diff -rs prk_tina_bak/ fds_tinav2.1/ | grep "相同$" > 3.txt

diff: prk_tina_bak/prebuilt/gcc/Linux-x86/aarch64/toolchain-sunxi/toolchain/aarch64-openwrt-Linux/lib/lib: 符号连接的层数过多

diff: fds_tinav2.1/prebuilt/gcc/Linux-x86/aarch64/toolchain-sunxi/toolchain/aarch64-openwrt-Linux/lib/lib: 符号连接的层数过多

diff: prk_tina_bak/prebuilt/gcc/Linux-x86/aarch64/toolchain-sunxi/toolchain/aarch64-openwrt-Linux/lib/lib64: recursive directory loop

diff: prk_tina_bak/prebuilt/gcc/Linux-x86/aarch64/toolchain-sunxi/toolchain/aarch64-openwrt-Linux/lib64/lib: 符号连接的层数过多

diff: fds_tinav2.1/prebuilt/gcc/Linux-x86/aarch64/toolchain-sunxi/toolchain/aarch64-openwrt-Linux/lib64/lib: 符号连接的层数过多

diff: prk_tina_bak/prebuilt/gcc/Linux-x86/aarch64/toolchain-sunxi/toolchain/aarch64-openwrt-Linux/lib64/lib64: recursive directory loop

diff: prk_tina_bak/prebuilt/gcc/Linux-x86/aarch64/toolchain-sunxi/toolchain/aarch64-openwrt-Linux-musl/lib/lib: 符号连接的层数过多

diff: fds_tinav2.1/prebuilt/gcc/Linux-x86/aarch64/toolchain-sunxi/toolchain/aarch64-openwrt-Linux-musl/lib/lib: 符号连接的层数过多

diff: prk_tina_bak/prebuilt/gcc/Linux-x86/aarch64/toolchain-sunxi/toolchain/aarch64-openwrt-Linux-musl/lib/lib64: recursive directory loop

diff: prk_tina_bak/prebuilt/gcc/Linux-x86/aarch64/toolchain-sunxi/toolchain/aarch64-openwrt-Linux-musl/lib64/lib: 符号连接的层数过多

diff: fds_tinav2.1/prebuilt/gcc/Linux-x86/aarch64/toolchain-sunxi/toolchain/aarch64-openwrt-Linux-musl/lib64/lib: 符号连接的层数过多

diff: prk_tina_bak/prebuilt/gcc/Linux-x86/aarch64/toolchain-sunxi/toolchain/aarch64-openwrt-Linux-musl/lib64/lib64: recursive directory loop

diff: prk_tina_bak/prebuilt/gcc/Linux-x86/aarch64/toolchain-sunxi/toolchain/lib/lib: 符号连接的层数过多

diff: fds_tinav2.1/prebuilt/gcc/Linux-x86/aarch64/toolchain-sunxi/toolchain/lib/lib: 符号连接的层数过多

diff: prk_tina_bak/prebuilt/gcc/Linux-x86/aarch64/toolchain-sunxi/toolchain/lib/lib64: recursive directory loop

diff: prk_tina_bak/prebuilt/gcc/Linux-x86/aarch64/toolchain-sunxi/toolchain/lib32/lib: 符号连接的层数过多

diff: fds_tinav2.1/prebuilt/gcc/Linux-x86/aarch64/toolchain-sunxi/toolchain/lib32/lib: 符号连接的层数过多

diff: prk_tina_bak/prebuilt/gcc/Linux-x86/aarch64/toolchain-sunxi/toolchain/lib32/lib64: recursive directory loop

diff: prk_tina_bak/prebuilt/gcc/Linux-x86/aarch64/toolchain-sunxi/toolchain/lib64/lib: 符号连接的层数过多

diff: fds_tinav2.1/prebuilt/gcc/Linux-x86/aarch64/toolchain-sunxi/toolchain/lib64/lib: 符号连接的层数过多

diff: prk_tina_bak/prebuilt/gcc/Linux-x86/aarch64/toolchain-sunxi/toolchain/lib64/lib64: recursive directory loop

diff: prk_tina_bak/prebuilt/gcc/Linux-x86/arm/toolchain-sunxi/toolchain/arm-openwrt-Linux/lib/lib: 符号连接的层数过多

diff: fds_tinav2.1/prebuilt/gcc/Linux-x86/arm/toolchain-sunxi/toolchain/arm-openwrt-Linux/lib/lib: 符号连接的层数过多

diff: prk_tina_bak/prebuilt/gcc/Linux-x86/arm/toolchain-sunxi/toolchain/arm-openwrt-Linux-muslgnueabi/lib/lib: 符号连接的层数过多

diff: fds_tinav2.1/prebuilt/gcc/Linux-x86/arm/toolchain-sunxi/toolchain/arm-openwrt-Linux-muslgnueabi/lib/lib: 符号连接的层数过多

diff: prk_tina_bak/prebuilt/gcc/Linux-x86/arm/toolchain-sunxi/toolchain/lib/lib: 符号连接的层数过多

diff: fds_tinav2.1/prebuilt/gcc/Linux-x86/arm/toolchain-sunxi/toolchain/lib/lib: 符号连接的层数过多

diff: prk_tina_bak/prebuilt/gcc/Linux-x86/arm/toolchain-sunxi/toolchain/lib32/lib: 符号连接的层数过多

diff: fds_tinav2.1/prebuilt/gcc/Linux-x86/arm/toolchain-sunxi/toolchain/lib32/lib: 符号连接的层数过多

diff: prk_tina_bak/prebuilt/gcc/Linux-x86/arm/toolchain-sunxi/toolchain/lib64/lib: 符号连接的层数过多

diff: fds_tinav2.1/prebuilt/gcc/Linux-x86/arm/toolchain-sunxi/toolchain/lib64/lib: 符号连接的层数过多

rootroot@cm-System-Product-Name:/home/wwt$

 

 

 

 

2、删除通过diff比对找到的相同的文件(同名同内容):

awk '{print $2}' 3.txt | xargs rm -rf

awk '{print $4}' 3.txt | xargs rm -rf

 

 

参考资料:

http://bbs.csdn.net/topics/320031351

awk如何删除一个文件?

 

awk -F, '{print $2}' test.txt | xargs rm -rf

 

 

 

 

3、你会发现有很多空的文件夹/目录,需要把它们找出来删除。

由于有些空文件夹里面只有空文件夹,当你删除最底层的空文件夹,上层的文件夹也是空的。

因此需要多次执行!

rootroot@cm-System-Product-Name:/home/wwt$ cd prk_tina_bak/

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$ find . -name "*" -type d -empty

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$

 

 

rootroot@cm-System-Product-Name:/home/wwt/prk_tina_bak$ cd ../fds_tinav2.1/

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$ find . -name "*" -type d -empty | xargs rm -rf

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$ find . -name "*" -type d -empty

rootroot@cm-System-Product-Name:/home/wwt/fds_tinav2.1$

 

 

参考资料:

http://www.111cn.net/sys/Linux/46433.htm

Linux中find批量删除空文件及空文件夹脚本

 

查询出所有的空文件夹

find -type d -empty

 

 

http://blog.csdn.net/u010555688/article/details/51280783

Ubuntu 删除两个文件夹下相同文件名的文件

参考:http://bbs.chinaunix.net/forum.PHP?mod=viewthread&tid=1851432&page=1

0

精彩评论

暂无评论...
验证码 换一张
取 消