查找目錄下同名但不同后綴名文件的shell腳本代碼
因?yàn)楹笈_(tái)錄入的同事,上傳文件的時(shí)候,給文件取了相同的名字,但不同的后綴名,由于文件路徑非常深,大概十層左右,每一層又有幾十個(gè)文件,所以人工找起來非常麻煩,所以寫了個(gè)腳本,幫他們實(shí)現(xiàn)查找指定目錄下所有子目錄及文件,找出相同文件名,不同后綴的文件,然后,手動(dòng)保留其中一個(gè)。
#!/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
模擬測試:
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í)用,分享一下
相關(guān)文章
expect實(shí)現(xiàn)單臺(tái)、多臺(tái)服務(wù)器批量scp傳輸文件
這篇文章主要介紹了expect實(shí)現(xiàn)單臺(tái)、多臺(tái)服務(wù)器批量scp傳輸文件,本文提供了單臺(tái)傳輸腳本、多臺(tái)傳輸腳本及服務(wù)器信息配置文件,需要的朋友可以參考下2014-12-12Shell四種運(yùn)行方式(啟動(dòng)方式)的實(shí)現(xiàn)
這篇文章主要介紹了Shell四種運(yùn)行方式(啟動(dòng)方式)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03解決bash:/root/.bashrc:Permission denied的問題
本文主要介紹了解決bash:/root/.bashrc:Permission denied的問題,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10linux 獲取某個(gè)日期對(duì)應(yīng)的月末日期方法
今天小編就為大家分享一篇linux 獲取某個(gè)日期對(duì)應(yīng)的月末日期方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06