Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if argument is a file or directory

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.

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
like image 300
Tomala Avatar asked Feb 01 '13 22:02

Tomala


People also ask

How do I know if an argument is a directory?

First check the provides argument is the directory or not using the if statement using the -d option for the first argument using the $1 parameter. If it is true then print the message that the provided argument is the directory. If the argument is not the directory then check it for the file.

How do you check if an argument is a file or directory in bash?

In order to check if a directory exists in Bash, you have to use the “-d” option and specify the directory name to be checked.

How do you check if a file is in a directory shell?

Checking If a Directory Exists In a Bash Shell Script-h "/path/to/dir" ] && echo "Directory /path/to/dir exists." || echo "Error: Directory /path/to/dir exists but point to $(readlink -f /path/to/dir)." The cmd2 is executed if, and only if, cmd1 returns a non-zero exit status.


1 Answers

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

like image 194
choroba Avatar answered Sep 22 '22 00:09

choroba