Find shell script code with the same name but different suffix file names in the directory _linux shell

Source: Internet
Author: User

Because the backstage input colleague, uploads the file the time, to file the same name, but different suffix name, because the file path is very deep, about 10 layers, each layer has dozens of files, so manually find a very troublesome, so write a script to help them find the specified directory of all subdirectories and files, Find files with the same file name, different suffixes, and then manually keep one of them.

Copy Code code as follows:

#!/bin/bash
#判断一下脚本参数的问题
If [$#-ne 1];then
echo "Usage find_same.sh Direcroty"
Exit
Fi
Find $1-type D >/tmp/dir.txt
#将所有需要查询的目录本身和子目录的名字存储在一个临时文件里
#对每个目录进行比较查询
While read Dir
Todo
Find $dir-maxdepth 1-type f >/tmp/file.txt
#将当前目录下的所有文件存储在临时文件里
Awk-f '/' {print $NF} '/tmp/file.txt | Awk-f ' [.] ' ' {print $} ' | Sort | uniq-d >/tmp/filename.txt
#把文件名字取出来, with the same name, put the name in the/tmp/filename.txt.
Line= ' Wc-l/tmp/filename.txt | awk ' {print '} '
#判断一下该文件里一共有多少行, each row is a filename with a duplicate name
#输出
echo "The directory $dir including same name file:"
If [$line-ge 1]; Then
While read name
Todo
Filename= ' grep $name/tmp/file.txt '
echo "$filename"
echo $filename >>/tmp/samefile.txt
#所有的记录存放在这个文件里
Done </tmp/filename.txt
Fi
Done </tmp/dir.txt

Simulation test:

Linux-8hij:/tmp/test # LL
Total 4
-rw-r--r--1 Root 0 Mar 9 02:04 1.png
-rw-r--r--1 Root 0 Mar 9 02:04 1.txt
Drwxr-xr-x 2 root 4096 Mar 9 02:05 test1
Linux-8hij:/tmp/test/test1 # LL
Total 0
-rw-r--r--1 Root 0 Mar 9 02:05 11.jpg
-rw-r--r--1 Root 0 Mar 9 02:05 11.log
-rw-r--r--1 Root 0 Mar 9 02:05 2.log

Run Result:

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

To view records:

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

This script can be used to achieve the specified directory under the same name but different suffix of the search, can be extended to delete the specified file script, feel very useful, share

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.