macOS shell 编程记

2. macOS 不支持 ll (需要使用 ls -l 实现),事实上 ll 在 linux 下是 ls -l 的快捷键。

  ls 显示当前目录下文件。

  ls -l 显示当前目录下文件详细信息。

#以下命令均不包含".",".."目录,以及"."开头的隐藏文件,如需包含,ll 需要加上 -a参数
#当前目录下文件个数,不包含子目录
ll |grep "^-"|wc -l
#当前目录下目录个数,不包含子目录
ll |grep "^d"|wc -l
#当前目录下文件个数,包含子目录
ll -R|grep "^-"|wc -l
#当前目录下目录个数,包含子目录
ll -R|grep "^d"|wc -l

1. 遍历目录

#!/bin/bash
function getdir(){
    for element in `ls $1`
    do  
        dir_or_file=$1"/"$element
        if [ -d $dir_or_file ]
        then 
            getdir $dir_or_file
        else
            echo $dir_or_file
        fi  
    done
}
root_dir="/home/test"
getdir $root_dir

猜你喜欢

转载自www.cnblogs.com/pinweyshg/p/9146352.html