濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > 查找目錄下同名但不同后綴名文件的shell腳本代碼

查找目錄下同名但不同后綴名文件的shell腳本代碼

熱門標(biāo)簽:昆明電話外呼系統(tǒng)好么 外呼系統(tǒng)一天耗費(fèi)多少流量 陜西便宜電銷機(jī)器人軟件 免費(fèi)門店地圖標(biāo)注注冊(cè)入駐 杞縣地圖標(biāo)注app 艾比利外呼系統(tǒng) 海口智能語音電銷機(jī)器人好用嗎 衡水外呼線路解決 電話機(jī)器人每天搜索多少次

因?yàn)楹笈_(tái)錄入的同事,上傳文件的時(shí)候,給文件取了相同的名字,但不同的后綴名,由于文件路徑非常深,大概十層左右,每一層又有幾十個(gè)文件,所以人工找起來非常麻煩,所以寫了個(gè)腳本,幫他們實(shí)現(xiàn)查找指定目錄下所有子目錄及文件,找出相同文件名,不同后綴的文件,然后,手動(dòng)保留其中一個(gè)。

復(fù)制代碼 代碼如下:

#!/bin/bash 
#判斷一下腳本參數(shù)的問題 
if [ $# -ne 1 ];then 
   echo "Usage find_same.sh direcroty" 
   exit 
fi 
find $1 -type d > /tmp/dir.txt 
#將所有需要查詢的目錄本身和子目錄的名字存儲(chǔ)在一個(gè)臨時(shí)文件里 
#對(duì)每個(gè)目錄進(jìn)行比較查詢 
while read dir 
do 
     find $dir -maxdepth 1 -type f > /tmp/file.txt 
     #將當(dāng)前目錄下的所有文件存儲(chǔ)在臨時(shí)文件里 
     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}'` 
     #判斷一下該文件里一共有多少行,每一行就是一個(gè)重名的文件名 
     #輸出 
     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 
                #所有的記錄存放在這個(gè)文件里 
        done /tmp/filename.txt 
      fi 
done /tmp/dir.txt

模擬測(cè)試:

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

運(yùn)行結(jié)果:

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

通過這個(gè)腳本可以實(shí)現(xiàn)指定目錄下同名但不同后綴名的查找,可以拓展為刪除指定的文件的腳本,覺得很實(shí)用,分享一下

您可能感興趣的文章:
  • shell對(duì)比文件內(nèi)容腳本分享
  • 批量轉(zhuǎn)換目錄下文件編碼的shell腳本代碼
  • linux下保留文件系統(tǒng)下剩余指定數(shù)目文件的shell腳本
  • shell去掉文件中空行(空白行)的方法詳解
  • shell判斷文件,目錄是否存在或者具有權(quán)限的代碼
  • 使用ShellClass獲取文件屬性詳細(xì)信息的實(shí)現(xiàn)方法
  • 判斷文件是否存在的shell腳本代碼
  • 合并一個(gè)文件夾下多個(gè)文件內(nèi)容的單行shell命令
  • 在指定目錄查找指定后綴文件的shell腳本代碼
  • shell查找當(dāng)前目錄下大于1M的文件的三種方法分享
  • 后臺(tái)實(shí)時(shí)分流文件的shell腳本
  • 找出文件中包含指定字段的文件的shell腳本
  • Visual Style中的shellstyle.dll文件修改方法
  • Byshell后門:無進(jìn)程無DLL無硬盤文件
  • shell替換文件中的文件路徑腳本分享

標(biāo)簽:泰安 昌都 臨滄 南京 西寧 宿遷 營(yíng)口

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《查找目錄下同名但不同后綴名文件的shell腳本代碼》,本文關(guān)鍵詞  查找,目,錄下,同名,但,不同,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《查找目錄下同名但不同后綴名文件的shell腳本代碼》相關(guān)的同類信息!
  • 本頁收集關(guān)于查找目錄下同名但不同后綴名文件的shell腳本代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    德清县| 舒兰市| 凤阳县| 左贡县| 遵义县| 昌都县| 鹤壁市| 梧州市| 高要市| 双鸭山市| 东乌| 远安县| 温宿县| 荔波县| 仙居县| 兴文县| 合山市| 邢台市| 六安市| 甘泉县| 麻栗坡县| 新和县| 绥滨县| 长兴县| 长治市| 阳曲县| 宁晋县| 台北市| 个旧市| 红原县| 本溪市| 金昌市| 凤冈县| 巩义市| 陆河县| 崇仁县| 封开县| 台南市| 天气| 富源县| 深州市|