shell如何判断文件or目录(文件夹)

主要用到两个shell命令:

-f "file"   :  判断file是否是文件;

-d "file"  :  判断file是否是目录(文件夹)。


#!/bin/sh

filename="/home"

if [ -d "$filename" ]; then

  echo "$filename is a directory "

elif [ -f "$filename" ]; then

  echo "$filename is a file"

fi

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure! Here's a shell script that accepts a directory as a parameter, recursively traverses all files and subdirectories within that directory, and outputs the count of `.log` and `.txt` files along with their respective file paths: ```bash #!/bin/bash # Function to recursively traverse directory traverse_directory() { local dir=$1 # Loop through each file and subdirectory in the given directory for file in "$dir"/*; do if [ -f "$file" ]; then # Check if the file has .log or .txt extension if [[ $file == *.log ]]; then echo "Log file found: $file" ((log_count++)) elif [[ $file == *.txt ]]; then echo "Text file found: $file" ((txt_count++)) fi elif [ -d "$file" ]; then # Recursively call the function for subdirectories traverse_directory "$file" fi done } # Check if directory is provided as argument if [ $# -eq 0 ]; then echo "Please provide a directory as an argument." exit 1 fi # Initialize file count variables log_count=0 txt_count=0 # Call the function to traverse the directory traverse_directory "$1" # Output the file counts echo "Number of log files: $log_count" echo "Number of text files: $txt_count" ``` To use this script, save it to a file (e.g., `file_count.sh`), make it executable using `chmod +x file_count.sh`, and then run it with the desired directory as the argument: ```bash ./file_count.sh /path/to/directory ``` Replace `/path/to/directory` with the actual directory path you want to traverse. The script will recursively search for `.log` and `.txt` files and print their counts along with the file paths.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值