shell中for循环文件路径查找

查看某目录下个文件的属性

for file in for file in /home/username*
do
if [ -d "$file" ]
then 
  echo "$file is directory"
elif [ -f "$file" ]
then
  echo "$file is file"
fi
done

输出结果:

/home/username/Desktop is directory
/home/username/Documents is directory
/home/username/Downloads is directory
/home/username/examples.desktop is file
/home/username/Music is directory
/home/username/Pictures is directory
/home/username/Public is directory
/home/username/snap is directory
/home/username/Templates is directory
/home/username/Videos is directory

补充:

if [ -f file ] 如果文件存在
if [ -d … ] 如果目录存在
if [ -s file ] 如果文件存在且非空
if [ -r file ] 如果文件存在且可读
if [ -w file ] 如果文件存在且可写
if [ -x file ] 如果文件存在且可执行

查看文件名称

for file in $(ls *.sh)
do
    echo $file is file path\!
done

输出结果:

test.sh is file path!

猜你喜欢

转载自blog.csdn.net/D_pens/article/details/81481798