In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use shell to write a script that recursively traverses directories and subdirectories". The content in the article is simple and clear, and it is easy to learn and understand. below, please follow Xiaobian's train of thought to slowly deepen, to study and learn "how to use shell to write recursive scripts to traverse directories and subdirectories".
Using the script written by shell to recursively traverse the directory, the script recursively traverses the specified directory and prints the file name under the directory.
Example 1:
The code is as follows:
#! / bin/sh
Function scandir () {
Local cur_dir parent_dir workdir
Workdir=$1
Cd ${workdir}
If [${workdir} = "/"]
Then
Cur_dir= ""
Else
Cur_dir=$ (pwd)
Fi
For dirlist in $(ls ${cur_dir})
Do
If test-d ${dirlist}; then
Cd ${dirlist}
Scandir ${cur_dir} / ${dirlist}
Cd..
Else
Echo ${cur_dir} / ${dirlist}
Fi
Done
}
If test-d $1
Then
Scandir $1
Elif test-f $1
Then
Echo "you input a file but not a directory,pls reinput and try again"
Exit 1
Else
Echo "the Directory isn't exist which you input,pls input a new Oneworld!"
Exit 1
Fi
Example 2: recursively read directories and their subdirectories
The code is as follows:
#! / bin/bash
Function read_dir () {
For file in `ls $1`
Do
If [- d $1 "/" $file] / / Note that a space must be added here, otherwise an error will be reported.
Then
Read_dir $1 "/" $file
Else
Echo $1 "/" $file
Fi
Done
}
# Test directory test
Read_dir test
In this way, the test.sh can be executed with execution permission.
The code is as follows:
Chmod + x test.sh
Sh test.sh
At this point, you can read the catalog file by passing parameters.
Example 3:
The code is as follows:
Recursively implement each subdirectory grandchild directory.
#! / bin/bash
# modify.func
Doit () / / handles non-directory files in the current directory and ignores directory files
{
Oldname= `ls | grep "$1 $" `
For name in $oldname
Do
If [- d "$name"]
Then:
Else
Basename= `echo $name | awk-F "."'{print $1}'`
Newname= "$basename$2"
Echo-e "$PWD/$name\ t\ t$newname"
Mv $name $newname
Count= `expr ${count} + 1`
Fi
Done
Return 0
}
Do_recursive () / / Recursively process each directory starting from the current directory
{
Doit $1 $2
For filename in `ls`
Do
If [- d "$filename"]
Then
Cd $filename
Do_recursive $1 $2
Cd..
Fi
Done
Return 0
}
Modify () / / processes the current directory and reports the result, which is equivalent to the main function, or you can call do_recursive directly
{
PARAMS=2
If [$#-ne $PARAMS]
Then
Echo "usage: mv_to .suf1 .suf2"
Return 1
Fi
Count=0
Do_recursive $1 $2
Echo "complete! $count files have been modified."
Return 0
}
Thank you for your reading, the above is the content of "how to use shell to write recursive scripts for traversing directories and subdirectories". After the study of this article, I believe you have a deeper understanding of how to use shell to write scripts for recursive traversing directories and subdirectories, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.