使用shell脚本查看文件类型

本文介绍了一个简单的Shell脚本,用于检测指定路径下的文件类型,并以不同颜色区分各种类型的文件,包括目录文件、普通文件、软链接文件等。
显示文件类型
#如查看   /etc 目录
[root@localhost ~]# sh test.sh /etc
/etc/                                             [目录文件]
#如查看   /etc 目录下所有文件
[root@localhost ~]# sh test.sh /etc/*
/etc/abrt                                         [目录文件]
/etc/adjtime                                      [普通文件]
/etc/aliases                                      [普通文件]
/etc/aliases.db                                   [普通文件]
/etc/alsa                                         [目录文件]
/etc/alternatives                                 [目录文件]
/etc/anacrontab                                   [普通文件]
............
脚本从这里开始,新建test.sh文件。
#!/bin/bash
for i in $@
do
    if [ -L "$i" ];then
        printf  "\e[36m%-50s[软链接文件]\e[0m\n" "$i"

    elif [ -f "$i" ];then
        printf  "\e[37m%-50s[普通文件]\e[0m\n" "$i"

    elif [ -d "$i" ];then
        printf  "\e[34m%-50s[目录文件]\e[0m\n" "$i"

    elif [ -c "$i" ];then
        printf  "\e[33m%-50s[字符设备文件]\e[0m\n" "$i"

    elif [ -b "$i" ];then
        printf  "\e[33m%-50s[设备块文件]\e[0m\n" "$i"

    elif [ -p "$i" ];then
        printf  "\e[33m%-50s[管道文件]\e[0m\n" "$i"

    elif [ -S "$i" ];then
        printf  "\e[35m%-50s[套接字文件]\e[0m\n" "$i"

    else
        printf  "\e[32m%-50s[其他格式文件]\e[0m\n" "$i"
    fi
done

转载于:https://www.cnblogs.com/outsrkem/p/11068816.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值