查找目录下同名但不同后缀名文件的shell脚本代码_linux shell

因为后台录入的同事,上传文件的时候,给文件取了相同的名字,但不同的后缀名,由于文件路径非常深,大概十层左右,每一层又有几十个文件,所以人工找起来非常麻烦,所以写了个脚本,帮他们实现查找指定目录下所有子目录及文件,找出相同文件名,不同后缀的文件,然后,手动保留其中一个。

复制代码 代码如下:

#!/bin/bash 
#判断一下脚本参数的问题 
if [ $# -ne 1 ];then 
   echo "Usage find_same.sh direcroty" 
   exit 
fi 
find $1 -type d > /tmp/dir.txt 
#将所有需要查询的目录本身和子目录的名字存储在一个临时文件里 
#对每个目录进行比较查询 
while read dir 
do 
     find $dir -maxdepth 1 -type f > /tmp/file.txt 
     #将当前目录下的所有文件存储在临时文件里 
     awk -F '/' '{print $NF}' /tmp/file.txt | awk -F '[.]' '{print $1}'| sort | uniq -d > /tmp/filename.txt 
     #把文件名字取出来,有同样名字的就把名字放到/tmp/filename.txt里 
     line=`wc -l /tmp/filename.txt | awk '{print $1}'` 
     #判断一下该文件里一共有多少行,每一行就是一个重名的文件名 
     #输出 
     echo "The directory $dir including same name file: " 
     if [ $line -ge 1 ] ; then 
        while read name 
        do 
        filename=`grep $name /tmp/file.txt` 
                echo "$filename" 
                echo $filename >> /tmp/samefile.txt 
                #所有的记录存放在这个文件里 
        done < /tmp/filename.txt 
      fi 
done < /tmp/dir.txt

模拟测试:

linux-8hij:/tmp/test # ll
total 4
-rw-r--r-- 1 root root    0 Mar  9 02:04 1.png
-rw-r--r-- 1 root root    0 Mar  9 02:04 1.txt
drwxr-xr-x 2 root root 4096 Mar  9 02:05 test1
linux-8hij:/tmp/test/test1 # ll
total 0
-rw-r--r-- 1 root root 0 Mar  9 02:05 11.jpg
-rw-r--r-- 1 root root 0 Mar  9 02:05 11.log
-rw-r--r-- 1 root root 0 Mar  9 02:05 2.log

运行结果:

linux-8hij:/tmp # ./find_name.sh /tmp
The directory /tmp including same name file:
The directory /tmp/.ICE-unix including same name file:
The directory /tmp/.X11-unix including same name file:
The directory /tmp/gconfd-root including same name file:
The directory /tmp/gconfd-root/lock including same name file:
The directory /tmp/gpg-PIEU09 including same name file:
The directory /tmp/test including same name file:
/tmp/test/1.txt
/tmp/test/1.png
The directory /tmp/test/test1 including same name file:
/tmp/test/test1/11.jpg
/tmp/test/test1/11.log

查看记录:

linux-8hij:/tmp # cat /tmp/samefile.txt
/tmp/test/1.txt /tmp/test/1.png
/tmp/test/test1/11.jpg /tmp/test/test1/11.log

通过这个脚本可以实现指定目录下同名但不同后缀名的查找,可以拓展为删除指定的文件的脚本,觉得很实用,分享一下

时间: 2024-08-06 21:32:05

查找目录下同名但不同后缀名文件的shell脚本代码_linux shell的相关文章

批量转换目录下文件编码的shell脚本代码_linux shell

一例批量转换目录下文件编码的shell脚本代码. 需求描述:由于从window转linux过来,很多原来win下的gbk文件需要转换成utf8. 以下脚本仅判断非utf8文件转换成utf8文件,并且默认非utf8文件为gbk,如果文件类型不一致需要修改. 例子: 复制代码 代码如下: #!/bin/bash# File Name: iconv.sh# Author: wanggy# site: www.jb51.net#show_file(){    for file in `ls $1`   

在指定目录查找指定后缀文件的shell脚本代码_linux shell

复制代码 代码如下: #!bin/sh  # 在指定位置查找指定后缀的文件,包括子目录  # 用法:  # findf $1 $2  # 第一个参数为后缀  # 查找指定后缀的文件并打印出来  # link:www.jb51.net# date:2013/2/26 f()  {    list=`find $2|grep "/.$1/>"`    for i in $list      do      echo $i    done  }  # 打印用法  print()  { 

Linux下一些常用的Shell脚本整理_linux shell

如何计算当前目录下的文件数和目录数         # ls -l * |grep "^-"|wc -l ---- to count files # ls -l * |grep "^d"|wc -l ----- to count dir         如何只列子目录?     ls -F | grep /$ 或者 alias sub = "ls -F | grep /$"(linux) ls -l | grep "^d" 或

Linux下实现SSH免密码登录和实现秘钥的管理、分发、部署SHELL脚本分享_linux shell

环境: ssh server: 192.168.100.29  server.example.com ssh client: 192.168.100.30  client.example.com 通过root用户建立秘钥认证实现SHELL脚本管理,分发,部署 首先client端创建秘钥对,并将公钥分发给需要登录的SSH服务端 注:公钥相当于锁,私钥相当于钥匙,我们这里相当于在客户端创建一对钥匙和锁,想要做到SSH免密码登录,就相当于我们将锁分发到服务端并装锁,然后客户端就可以利用钥匙开锁. 一.

CentOS下mysql定时备份Shell脚本分享_linux shell

1.备份语句  复制代码 代码如下: # /usr/local/mysql/bin/mysqldump -utest -ptest test --socket=/tmp/mysql.3306.sock > /usr/local/mysql_backup/3306/test-`date +%Y%m%d`.sql.gz  语句说明:   /usr/local/mysql/bin/mysqldump:MySql安装目录下 -utest:分为两块,一块为-u一块为test:其中-u表示其为用户名,tes

linux中查找目录下包含某个类的jar文件

支持cygwin(两种路径格式都支持),支持相对路径 #!/bin/bash #从目录中的jar文件中查找指定的类 path=$1 name=$2 if [ "" = "$path" ];then  echo "path not specified"  exit fi if [ "" = "$name" ];then  echo "name not specified"  exit 1

Ubuntu下定时提交代码到SVN服务器的Shell脚本分享_linux shell

有两个用途,一是偷懒不用去提交代码,二是在远程服务器上使用以作定期备份. 复制代码 代码如下: sudo gedit /etc/crontab 打开后加入这行 复制代码 代码如下: 0 * * * * my_user_name    my_script_path 表示每个整点以my_user_name身份执行my_script_path.这里最好是使用当前的用户名,修改掉默认的root,否则可能以root身份会提交失败.详情请询问百度crontab的用法. 然后script的内容为: 复制代码

shell遍历目录处理特定目录的脚本代码_linux shell

需求:删除文件,目录结构为:ip/年份/月份,每个人都有一份这样的目录现需要删除2012年以前的所有文件,2012年以后的删除特定月份.本来想用find,结果发现这些文件time(a,m,c)记录已经不是当初生成的.没办法只好想了个土办法去遍历,哪位有好的方法,欢迎告知. 复制代码 代码如下: #!/bin/bashcd /home/wanggy/rmfile/ls  -l | grep ^d | awk '{print $NF}'  >/home/wanggy/tmpcat /home/wan

python查找目录下指定扩展名的文件实例_python

本文实例讲述了python查找目录下指定扩展名的文件.分享给大家供大家参考.具体如下: 这里使用python查找当前目录下的扩展名为.txt的文件 import os items = os.listdir(".") newlist = [] for names in items: if names.endswith(".txt"): newlist.append(names) print newlist 希望本文所述对大家的Python程序设计有所帮助. 以上是小编