What command can be used to check if a directory exists or not, within a shell script?
问题:
回答1:
To check if a directory exists in a shell script you can use the following:
if [ -d \"$DIRECTORY\" ]; then
# Control will enter here if $DIRECTORY exists.
fi
Or to check if a directory doesn\'t exist:
if [ ! -d \"$DIRECTORY\" ]; then
# Control will enter here if $DIRECTORY doesn\'t exist.
fi
However, as Jon Ericson points out, subsequent commands may not work as intended if you do not take into account that a symbolic link to a directory will also pass this check. E.g. running this:
ln -s \"$ACTUAL_DIR\" \"$SYMLINK\"
if [ -d \"$SYMLINK\" ]; then
rmdir \"$SYMLINK\"
fi
Will produce the error message:
rmdir: failed to remove `symlink\': Not a directory
So symbolic links may have to be treated differently, if subsequent commands expect directories:
if [ -d \"$LINK_OR_DIR\" ]; then
if [ -L \"$LINK_OR_DIR\" ]; then
# It is a symlink!
# Symbolic link specific commands go here.
rm \"$LINK_OR_DIR\"
else
# It\'s a directory!
# Directory command goes here.
rmdir \"$LINK_OR_DIR\"
fi
fi
Take particular note of the double-quotes used to wrap the variables, the reason for this is explained by 8jean in another answer.
If the variables contain spaces or other unusual characters it will probably cause the script to fail.
回答2:
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.
if [ -d \"$DIRECTORY\" ]; then
# Will enter here if $DIRECTORY exists, even if it contains spaces
fi
回答3:
Note the -d test can produce some surprising results:
$ ln -s tmp/ t
$ if [ -d t ]; then rmdir t; fi
rmdir: directory \"t\": Path component not a directory
File under: \"When is a directory not a directory?\" The answer: \"When it\'s a symlink to a directory.\" A slightly more thorough test:
if [ -d t ]; then
if [ -L t ]; then
rm t
else
rmdir t
fi
fi
You can find more information in the Bash manual on Bash conditional expressions and the [
builtin command and the [[
compound commmand.
回答4:
I find the double-bracket version of test
makes writing logic tests more natural:
if [[ -d \"${DIRECTORY}\" && ! -L \"${DIRECTORY}\" ]] ; then
echo \"It\'s a bona-fide directory\"
fi
回答5:
Shorter form:
[ -d \"$DIR\" ] && echo \"Yes\"
回答6:
To check if a directory exists you can use simple if structure like this:
if [ -d directory/path to a directory ] ; then
#Things to do
else #if needed #also: elif [new condition]
# things to do
fi
You can do it also in negative
if [ ! -d directory/path to a directory ] ; then
# things to do when not an existing directory
Note: Be careful, leave empty spaces on either side of both opening and closing braces.
With the same syntax you can use:
-e: any kind of archive
-f: file
-h: symbolic link
-r: readable file
-w: writable file
-x: executable file
-s: file size greater than zero
回答7:
if [ -d \"$DIRECTORY\" ]; then
# Here if $DIRECTORY exists
fi
回答8:
You can use test -d
(see man test
).
-d file
True if file exists and is a directory.
For example:
test -d \"/etc\" && echo Exists || echo Does not exist
Note: The test
command is same as conditional expression [
(see: man [
), so it\'s portable across shell scripts.
[
- This is a synonym for thetest
builtin, but the last argument must, be a literal]
, to match the opening[
.
For possible options or further help, check:
help [
help test
man test
orman [
回答9:
Or for something completely useless:
[ -d . ] || echo \"No\"
回答10:
Here\'s a very pragmatic idiom:
(cd $dir) || return # is this a directory,
# and do we have access?
I typically wrap it in a function:
can_use_as_dir() {
(cd ${1:?pathname expected}) || return
}
Or:
assert_dir_access() {
(cd ${1:?pathname expected}) || exit
}
The nice thing about this approach is that I do not have to think of a good error message.
cd
will give me a standard one line message to stderr already. It will also give more information than I will be able to provide. By performing the cd
inside a subshell ( ... )
, the command does not affect the current directory of the caller. If the directory exists, this subshell and the function are just a no-op.
Next is the argument that we pass to cd
: ${1:?pathname expected}
. This is a more elaborate form of parameter substitution which is explained in more detail below.
Tl;dr: If the string passed into this function is empty, we again exit from the subshell ( ... )
and return from the function with the given error message.
Quoting from the ksh93
man page:
${parameter:?word}
If
parameter
is set and is non-null then substitute its value; otherwise, printword
and exit from the shell (if not interactive). Ifword
is omitted then a standard message is printed.
and
If the colon
:
is omitted from the above expressions, then the shell only checks whether parameter is set or not.
The phrasing here is peculiar to the shell documentation, as word
may refer
to any reasonable string, including whitespace.
In this particular case, I know that the standard error message 1: parameter not set
is not sufficient, so I zoom in on the type of value that we expect here - the pathname
of a directory.
A philosphical note:
The shell is not an object oriented language, so the message says pathname
, not directory
. At this level, I\'d rather keep it simple - the arguments to a function are just strings.
回答11:
A simple script to test if dir or file is present or not:
if [ -d /home/ram/dir ] # for file \"if [-f /home/rama/file]\" then echo \"dir present\" else echo \"dir not present\" fi
A simple script to check whether the directory is present or not:
mkdir tempdir # if you want to check file use touch instead of mkdir ret=$? if [ \"$ret\" == \"0\" ] then echo \"dir present\" else echo \"dir not present\" fi
The above scripts will check the dir is present or not
$?
if the last command sucess it returns \"0\" else non zero value. supposetempdir
is already present thenmkdir tempdir
will give error like below:mkdir: cannot create directory ‘tempdir’: File exists
回答12:
if [ -d \"$Directory\" -a -w \"$Directory\" ]
then
#Statements
fi
The above code checks if the directory exists and if it is writable.
回答13:
Type this code on the bash promt
if [ -d \"$DIRECTORY\" ]; then
# if true this block of code will execute
fi
回答14:
More features using find
Check existence of the folder within sub-directories:
found=`find -type d -name \"myDirectory\"` if [ -n \"$found\"] then # The variable \'found\' contains the full path where \"myDirectory\" is. # It may contain several lines if there are several folders named \"myDirectory\". fi
Check existence of one or several folders based on a pattern within the current directory:
found=`find -maxdepth 1 -type d -name \"my*\"` if [ -n \"$found\"] then # The variable \'found\' contains the full path where folders \"my*\" have been found. fi
Both combinations. In the following example, it checks the existence of the folder in the current directory:
found=`find -maxdepth 1 -type d -name \"myDirectory\"` if [ -n \"$found\"] then # The variable \'found\' is not empty => \"myDirectory\"` exists. fi
回答15:
Actually, you should use several tools to get a bulletproof approach:
DIR_PATH=`readlink -f \"${the_stuff_you_test}\"` # Get rid of symlinks and get abs path
if [[ -d \"${DIR_PATH}\" ]] ; Then # now you\'re testing
echo \"It\'s a dir\";
fi
No need to worry about spaces and special characters as long as you use \"${}\"
.
Note that [[]]
is not as portable as []
, but since most people work with modern versions of Bash (since after all, most people don\'t even work with command line :-p), the benefit is greater than the trouble.
回答16:
To check more than one directory use this code:
if [ -d \"$DIRECTORY1\" ] && [ -d \"$DIRECTORY2\" ] then
# Things to do
fi
回答17:
Have you considered just doing whatever you want to do in the if
rather than looking before you leap?
IE, if you want to check for the existence of a directory before you enter it, try just doing this:
if pushd /path/you/want/to/enter; then
# commands you want to run in this directory
popd
fi
If the path you give to pushd
exists, you\'ll enter it and it\'ll exit with 0
, which means the then
portion of the statement will execute. If it doesn\'t exist, nothing will happen (other than some output saying the directory doesn\'t exist, which is probably a helpful side-effect anyways for debugging).
Seems better than this, which requires repeating yourself:
if [ -d /path/you/want/to/enter ]; then
pushd /path/you/want/to/enter
# commands you want to run in this directory
popd
fi
Same thing works with cd
, mv
, rm
, etc... if you try them on files that don\'t exist, they\'ll exit with an error and print a message saying it doesn\'t exist, and your then
block will be skipped. If you try them on files that do exist, the command will execute and exit with a status of 0
, allowing your then
block to execute.
回答18:
[[ -d \"$DIR\" && ! -L \"$DIR\" ]] && echo \"It\'s a directory and not a symbolic link\"
N.B: Quoting variables is a good practice.
回答19:
Check if directory exists, else make one
[ -d \"$DIRECTORY\" ] || mkdir $DIRECTORY
回答20:
[ -d ~/Desktop/TEMPORAL/ ] && echo \"DIRECTORY EXISTS\" || echo \"DIRECTORY DOES NOT EXIST\"
回答21:
This answer wrapped up as a shell script
Examples
$ is_dir ~
YES
$ is_dir /tmp
YES
$ is_dir ~/bin
YES
$ mkdir \'/tmp/test me\'
$ is_dir \'/tmp/test me\'
YES
$ is_dir /asdf/asdf
NO
# Example of calling it in another script
DIR=~/mydata
if [ $(is_dir $DIR) == \"NO\" ]
then
echo \"Folder doesnt exist: $DIR\";
exit;
fi
is_dir
function show_help()
{
IT=$(CAT <<EOF
usage: DIR
output: YES or NO, depending on whether or not the directory exists.
)
echo \"$IT\"
exit
}
if [ \"$1\" == \"help\" ]
then
show_help
fi
if [ -z \"$1\" ]
then
show_help
fi
DIR=$1
if [ -d $DIR ]; then
echo \"YES\";
exit;
fi
echo \"NO\";
回答22:
Using the -e
check will check for files and this includes directories.
if [ -e ${FILE_PATH_AND_NAME} ]
then
echo \"The file or directory exists.\"
fi
回答23:
As per Jonathan comment:
If you want to create the directory and it does not exist yet, then the simplest technique is to use mkdir -p
which creates the directory — and any missing directories up the path — and does not fail if the directory already exists, so you can do it all at once with:
mkdir -p /some/directory/you/want/to/exist || exit 1
回答24:
if [ -d \"$DIRECTORY\" ]; then
# Will enter here if $DIRECTORY exists
fi
This is not completely true... If you want to go to that directory, you also needs to have the execute rights on the directory. Maybe you need to have write rights as well.
Therfore:
if [ -d \"$DIRECTORY\" ] && [ -x \"$DIRECTORY\" ] ; then
# ... to go to that directory (even if DIRECTORY is a link)
cd $DIRECTORY
pwd
fi
if [ -d \"$DIRECTORY\" ] && [ -w \"$DIRECTORY\" ] ; then
# ... to go to that directory and write something there (even if DIRECTORY is a link)
cd $DIRECTORY
touch foobar
fi
回答25:
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 a d
or a -
(dash). In case of a d
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:
[[ $(ls -ld \"$ISDIR\" | cut -c1) == \'d\' ]] &&
echo \"YES, $ISDIR is a directory.\" ||
echo \"Sorry, $ISDIR is not a directory\"
Practical usage:
[claudio@nowhere ~]$ ISDIR=\"$HOME/Music\"
[claudio@nowhere ~]$ ls -ld \"$ISDIR\"
drwxr-xr-x. 2 claudio claudio 4096 Aug 23 00:02 /home/claudio/Music
[claudio@nowhere ~]$ [[ $(ls -ld \"$ISDIR\" | cut -c1) == \'d\' ]] &&
echo \"YES, $ISDIR is a directory.\" ||
echo \"Sorry, $ISDIR is not a directory\"
YES, /home/claudio/Music is a directory.
[claudio@nowhere ~]$ touch \"empty file.txt\"
[claudio@nowhere ~]$ ISDIR=\"$HOME/empty file.txt\"
[claudio@nowhere ~]$ [[ $(ls -ld \"$ISDIR\" | cut -c1) == \'d\' ]] &&
echo \"YES, $ISDIR is a directory.\" ||
echo \"Sorry, $ISDIR is not a directoy\"
Sorry, /home/claudio/empty file.txt is not a directory
回答26:
file=\"foo\"
if [[ -e \"$file\" ]]; then echo \"File Exists\"; fi;
回答27:
If you want to check if a directory exists, regardless if it\'s a real directory or a symlink, use this:
ls $DIR
if [ $? != 0 ]; then
echo \"Directory $DIR already exists!\"
exit 1;
fi
echo \"Directory $DIR does not exist...\"
Explanation: The \"ls\" command gives an error \"ls: /x: No such file or directory\" if the directory or symlink does not exist, and also sets the return code, which you can retrieve via \"$?\", to non-null (normally \"1\"). Be sure that you check the return code directly after calling \"ls\".
回答28:
(1)
[ -d Piyush_Drv1 ] && echo \"\"Exists\"\" || echo \"Not Exists\"
(2)
[ `find . -type d -name Piyush_Drv1 -print | wc -l` -eq 1 ] && echo Exists || echo \"Not Exists\"
(3)
[[ -d run_dir && ! -L run_dir ]] && echo Exists || echo \"Not Exists\"
If found an issue with one of the approach provided above.
With ls
command; the cases when directory does not exists - an error message is shown
$ [[ ls -ld SAMPLE_DIR| grep ^d | wc -l
-eq 1 ]] && echo exists || not exists
-ksh: not: not found [No such file or directory]
回答29:
Great solutions out there, but ultimately every script will fail if you\'re not in the right directory. So code like this:
if [ -d \"$LINK_OR_DIR\" ]; then
if [ -L \"$LINK_OR_DIR\" ]; then
# It is a symlink!
# Symbolic link specific commands go here
rm \"$LINK_OR_DIR\"
else
# It\'s a directory!
# Directory command goes here
rmdir \"$LINK_OR_DIR\"
fi
fi
will execute successfully only if at the moment of execution you\'re in a directory that has a subdirectory that you happen to check for.
I understand the initial question like this: to verify if a directory exists irrespective of the user\'s position in the file system. So using the command \'find\' might do the trick:
dir=\" \"
echo \"Input directory name to search for:\"
read dir
find $HOME -name $dir -type d
This solution is good because it allows the use of wildcards, a useful feature when searching for files/directories. The only problem is that, if the searched directory doesn\'t exist, the \'find\' command will print nothing to stdout (not an elegant solution for my taste) and will have nonetheless a zero exit. Maybe someone could improve on this.
回答30:
Below find can be used,
find . -type d -name dirname -prune -print