What command can be used to check if a directory exists or not, within a shell script?
相关问题
- How to get the return code of a shell script in lu
- Is shmid returned by shmget() unique across proces
- Invoking Mirth Connect CLI with Powershell script
- Why should we check WIFEXITED after wait in order
- Emacs shell: save commit message
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- In IntelliJ IDEA, how can I create a key binding t
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- Making new files automatically executable?
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- What does it take to be durable on Linux?
The
ls
command in conjunction with-l
(long listing) option returns attributes information about files and directories.In particular the first character of
ls -l
output it is usually ad
or a-
(dash). In case of ad
the one listed is a directory for sure.The following command in just one line will tell you if the given
ISDIR
variable contains a path to a directory or not:Practical usage:
Type this code on the bash promt
Remember to always wrap variables in double quotes when referencing them in a bash script. Kids these days grow up with the idea that they can have spaces and lots of other funny characters in their directory names. (Spaces! Back in my days, we didn't have no fancy spaces! ;))
One day, one of those kids will run your script with
$DIRECTORY
set to"My M0viez"
and your script will blow up. You don't want that. So use this.Note the -d test can produce some surprising results:
File under: "When is a directory not a directory?" The answer: "When it's a symlink to a directory." A slightly more thorough test:
You can find more information in the Bash manual on Bash conditional expressions and the
[
builtin command and the[[
compound commmand.To check more than one directory use this code:
Below find can be used,