用Shell 脚本 实现 相同路径或文件夹下重复文件的查找
来源:互联网 发布:ubuntu edk2 下载 编辑:程序博客网 时间:2024/06/07 18:13
有一次,我在使用Google drive时不小心将已经存放有文件的文件夹设置为了google drive 的专有文件夹。
Google drive 的同步服务将目录中已经存在的文件也下载了下来,而且自动重命名为xxx.(1) 这样的形式。于是有些子目录内存在大量的内容相同,名字不同的文件。
由于我的网盘目录结构复杂,文件数量大,如果手工找出这些重名文件并删除的话很耗费精力。于是自己动手编写了一个shell 脚本程序来帮我的忙。
该脚本通过对比相同路径下的文件内容来自动找出相同路径下的重复文件(内容相同)然后将他们按照之前的目录结构 全部复制到当前路径下的DuplicatedFiles 文件夹下。
#!/bin/bash#Delete duplicated files in one directory recursivelyfunction ergodicFolder(){for file in `ls "$1" | tr " " "\?"`dofile=`tr "\?" "\ " <<<$file`local currentDirectory=$1if [ -d "$currentDirectory/$file" ]then ergodicFolder "$currentDirectory/$file"elsecurrentDirectory=$1#local path="$1""/" #get the full directory of the filelocal sourceFile="$file" #get the file namesecho "###Processing in directory: $currentDirectory"for file2 in `ls "$currentDirectory" | tr " " "\?"`dofile2=`tr "\?" "\ " <<<$file2`local targetFile="$file2"local innerPath="$currentDirectory"# If the target file and source file existif [ -f "$innerPath/$targetFile" ]&&[ -f "$innerPath/$sourceFile" ]then#skip the same fileif [ "$targetFile" == "$sourceFile" ]then continue fi#skip the directoryif [ -d "$innerPath/$targetFile" ]then#delete recursively#ergodicFolder "$innerPath$targetFile"continuefi#compare and delete the same file which has long name if [ "`cmp "$innerPath/$sourceFile" "$innerPath/$targetFile"`" == "" ]thenecho "***(""$innerPath/$sourceFile"") is as same as (""$innerPath/$targetFile"")"local longNameFileWithPath="$innerPath/$sourceFile" if [ `expr length "$sourceFile"` -ge `expr length "$targetFile"` ]thenlongNameFileWithPath="$innerPath/$sourceFile" elselongNameFileWithPath="$innerPath/$targetFile" fitar -c "$longNameFileWithPath" | tar x -C "$targetDirectory"echo "delete file:"$longNameFileWithPath" -------" rm "$longNameFileWithPath"fifidonefidone}if [ ! -d "`pwd`/DuplicatedFiles" ]thenmkdir "`pwd`/DuplicatedFiles"fitargetDirectory="`pwd`/DuplicatedFiles"ergodicFolder "$1"
讨论:
1.如果需要对找到的文件进行其他操作请自行替换 tar -c "$longNameFileWithPath" | tar x -C "$targetDirectory" 这一句。
2.其实如果在windows 平台下被自动重名名了的文件,可以用正则表达式匹配文件名以(1)结尾的文件来达到目的。不过只是通过文件名进行判断的话并不是很安全的做法哦。
3.这个算法对相同路径下的文件实行两两对比,个人觉得算法效率还有提升空间。欢迎各位有更好想法的留言讨论。
- 用Shell 脚本 实现 相同路径或文件夹下重复文件的查找
- shell脚本实现查找文件夹下重复的文件,并提供删除功能
- shell脚本查找两个文件夹下相同的文件名
- shell脚本---查找并删除重复的文件
- shell脚本自动查找指定路径下包含某关键字的所有文件
- shell脚本自动查找指定路径下包含某关键字的所有文件
- Linux 定时删除文件或文件夹的shell脚本
- 查找头文件中相同函数声明--用shell实现
- 查找指定目录下的文件夹或文件
- Linux 下查找文件或文件夹
- shell 脚本实现文件的随机无重复筛选
- Shell脚本给某路径下的文件可读权限
- 用DirectoryExists和FileExists判断指定路径下是否存在指定的文件夹或文件
- 利用Linux查找重复文件(shell脚本)
- 用Shell脚本来遍历文件夹里的所有文件
- shell脚本——通过关键字查找文件路径
- 偷梁换柱:用脚本实现共享文件夹路径的转换
- 实现一个文件夹同步的shell脚本
- 制作根文件系统:make[1]: *** [miscutils/ubi_tools.o] 错误 1 --来源于网络
- HEVC中SAO--自适应样点补偿 详细分析解读
- 图片异步加载框架 Android-Universal-Image-Loader
- VC2008 Express下安装OpenCV2.31
- Uva11419 I AM SAM 我是山姆 二分匹配
- 用Shell 脚本 实现 相同路径或文件夹下重复文件的查找
- excel 导出输出格式已经固定
- 最长重复子串
- 用 shell 脚本批量下载畅想听吧(CXT8)和 有声下吧(YSX8)的有声小说
- unsigned int
- 2、 excel 导出。这种输入方式,适合后台输出,不需要打开EXCEL文件,不需要选择输出路径,只是后台自己默默的记录EXCEL文件。
- uva 10870 - Recurrences(矩阵乘法)
- python list 去重
- java泛型