The script that recursively iterates through the directory with the shell writes recursively traverses the specified directory and prints the file name under the directory.
Example 1:
Copy Code code 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})
Todo
If test-d ${dirlist};then
CD ${dirlist}
Scandir ${cur_dir}/${dirlist}
Cd..
Else
Echo ${cur_dir}/${dirlist}
Fi
Done
}
If test-d $
Then
Scandir $
Elif Test-f $
Then
echo "You are 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 one!!"
Exit 1
Fi
Instance 2: Recursive reading of directories and their subdirectories
Copy Code code as follows:
#! /bin/bash
function Read_dir () {
For file in ' ls $ '
Todo
If [-D $/$file]//Note there must be a space between here, or you will be able to error
Then
Read_dir "/" $file
Else
echo $ "/" $file
Fi
Done
}
#测试目录 Test
Read_dir Test
This gives test.sh the Execute permission to execute
Copy Code code as follows:
chmod +x test.sh
SH test.sh
This allows you to read the catalog file by passing parameters.
Example 3:
Copy Code code as follows:
Recursive implementation of each subdirectory of the subdirectories ...
#!/bin/bash
#modify. Func
doit ()//processing of non-directory files in the current directory, ignoring directory files
{
Oldname= ' ls | grep "$1$" '
For name in $oldname
Todo
If [-D "$name]"
Then:
Else
Basename= ' echo $name | Awk-f "." ' {print '} '
Newname= "$basename $"
Echo-e "$PWD/$name \t\t$newname"
MV $name $newname
count= ' expr ${count} + 1 '
Fi
Done
return 0
}
Do_recursive ()//starting from current directory, recursively processing each directory
{
Doit $ $
for filename in ' ls '
Todo
If [-D "$filename]"
Then
CD $filename
Do_recursive $ $
Cd..
Fi
Done
return 0
}
Modify ()//process The current directory and report the result, which is equivalent to the main function, and can be directly invoked do_recursive
{
params=2
If [$#-ne $PARAMS]
Then
echo "usage:mv_to. Suf1. Suf2"
Return 1
Fi
Count=0
Do_recursive $ $
echo "complete! $count files have been modified. "
return 0
}