The shell gets the names of all the folders under the directory and outputs

Source: Internet
Author: User
Gets the names of all the folders under the specified directory/usr/and outputs:

Shell code:

1 #! / Bin / bash
  2 #Method 1
  3 dir = $ (ls -l / usr / | awk '/ ^ d / {print $ NF}')
  4 for i in $ dir
  5 do
  6 echo $ i
  7 done
  8 #######
  9 #Method Two
10 for dir in $ (ls / usr /)
11 do
12 [-d $ dir] && echo $ dir
13 done
14 ## Method Three
15
16 ls -l / usr / | awk '/ ^ d / {print $ NF}' ## Actually the same as the first method, you can directly display without for loop

Running the shell will output the names of all the folders in the/usr/directory:

 1 [root@localhost ~]# ./foler.sh
 2 bin
 3 etc
 4 games
 5 include
 6 lib
 7 lib64
 8 libexec
 9 local
10 sbin
11 share
12 src


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.