Shell traverses all files under the directory and its subdirectories

Source: Internet
Author: User


The shell script is so powerful that it always touches the tip of the iceberg.

Shell recursively traverse directory

#!/bin/bash
echo "Please input the directory:"
Read Cdir
For Dir in $ (find $cdir-type D); Todo
echo Change to $dir
CD $dir
Ls
echo "----------"
Done
CD $cdir

Today, log a script that traverses all subdirectories and their files in the directory.


#!/bin/sh

Read-p "Input Path:" spath
FileList () {
filelist= ' ls $SPATH '
for filename in $filelist; Todo
If [f $filename];then
echo File: $filename
elif [D-$filename];then
#echo Directory: $filename
CD $filename
Spath= ' pwd '
Echo $SPATH
FileList
Cd..
Else
echo "$SPATH/$filename is not a common file."
Fi
Done
}

CD $SPATH
FileList

echo "Done."

The traversal of a directory and its subdirectories is implemented by defining a function.

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.