Linux 检查参数是文件还是目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14656080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:53:59  来源:igfitidea点击:

Check if argument is a file or directory

linuxbashshellunixubuntu-12.04

提问by Tomala

I am trying to check if an argument is a directory or a file. I want to put a / after each directory name and a * after every executable file. I know ls uses -F to get this information but I can't figure this out in my script.

我正在尝试检查参数是目录还是文件。我想在每个目录名后面放一个 / ,在每个可执行文件后面放一个 * 。我知道 ls 使用 -F 来获取此信息,但我无法在我的脚本中弄清楚这一点。

Here is my code:

这是我的代码:

    echo -n "Please enter Directory name you wish to search: "
read dir

for filename in "/home/me/Desktop/$dir"/*

do

    if (-F $filename)
    then 
    echo $filename

    fi
done

采纳答案by choroba

[ -f "$filename" ]is true for files, [ -d "$dirname" ]is true for directories.

[ -f "$filename" ]对于文件[ -d "$dirname" ]是这样,对于目录也是这样。