Shell script recursive traversal directory and subdirectory example share _linux shell

Source: Internet
Author: User

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

}

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.