Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find the shell script code of files with the same name but different suffixes in the directory

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to find the shell script code of the same name but different suffix files under the directory, the article is very detailed, has a certain reference value, interested friends must read it!

Because the colleague who entered the file in the background gave the file the same name, but different suffix names, because the file path is very deep, about ten layers, and there are dozens of files on each layer, so it is very troublesome to find it manually. So I wrote a script to help them find all the subdirectories and files under the specified directory, find the files with the same file name and different suffixes, and then Keep one of them manually.

The code is as follows:

#! / bin/bash

# determine the problem with the script parameters

If [$#-ne 1]; then

Echo "Usage find_same.sh direcroty"

Exit

Fi

Find $1-type d > / tmp/dir.txt

# store the names of all directories and subdirectories that need to be queried in a temporary file

# compare and query each directory

While read dir

Do

Find $dir-maxdepth 1-type f > / tmp/file.txt

# store all files in the current directory in temporary files

Awk-F'/'{print $NF}'/ tmp/file.txt | awk-F'[.]'{print $1}'| sort | uniq-d > / tmp/filename.txt

# take out the name of the file, and put the name in / tmp/filename.txt if you have the same name

Line= `wc-l / tmp/filename.txt | awk'{print $1}'`

# determine how many lines there are in the file. Each line is a file name with the same name.

# output

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

# all records are stored in this file

Done < / tmp/filename.txt

Fi

Done < / tmp/dir.txt

Simulation test:

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

Running 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

View the record:

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

Through this script, you can find the same name but different suffixes under the specified directory, and can be extended to delete the specified file script.

The above is all the contents of the article "how to find the shell script code of files with the same name but different suffixes in the directory". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report