shell脚本查找指定目录下所有子目录中的同名文件


shell脚本查找指定目录下所有子目录中的同名文件
 
前言
今天qq群里有位挺厉害的大神提出了一个问题:“上海-redis-蛋疼(137795882) 17:39:37  有没有查找一个目录下面 有没有重名文件的 工具 啊”
大家都知道,同一个目录下是不存在同名文件的,因此肯定要遍历子目录查找同名文件
  www.2cto.com  
思路
find命令查找所有的子目录
find命令循环遍历子目录,获取所有的文件
对所有的文件做去重
因为是shell脚本,所以用好sort、uniq、awk等是非常方便的
 
SHELL脚本
[html] 
#!/bin/bash  
  
#获取查找的目录名  
if [ 'x' == 'x'$1 ]; then  
    echo "Usage $0 search_dir"  
    exit  
fi  
  
#变量定义  
dir_arr=$(find $1 -type d -print;)  
store_path="/tmp/1.txt"  
if [ -f $store_path ]; then  
    rm -r $store_path  
fi  
  
#获取所有的文件  
for dir in ${dir_arr[*]}  
do  
    find $dir -type f >>$store_path  
done  
  
#输出重复的文件名  
for file in $(awk -F '/' '{print $NF}' $store_path  | sort | uniq -d)  
do  
    echo $file  
done  
 

相关内容

    暂无相关文章