2018年5月3日 星期四

[bash] 取得某路徑下的第一層子目錄名稱列表

list directory names in bash shell | Bash

#!/bin/bash

MYDIR="/home/ntpc"

DIRS=`ls -l $MYDIR --time-style="long-iso" | egrep '^d' | awk '{print $8}'`

# "ls -l $MYDIR"      = get a directory listing
# "| egrep '^d'"           = pipe to egrep and select only the directories
# "awk '{print $8}'" = pipe the result from egrep to awk and print only the 8th field

# and now loop through the directories:
for DIR in $DIRS
do
echo  ${DIR}
done

沒有留言:

張貼留言