You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Note:
Symbolic links may have to be treated differently, if subsequent commands expect directories:
if [ -d"$LINK_OR_DIR" ];thenif [ -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"fifi
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.
If Environment Variable is Set
[ -z"$VARIABLE" ] && VARIABLE="abc"if env | grep -q ^VARIABLE=
thenecho env variable is already exported
elseecho env variable was not exported, but now it is
export VARIABLE
fi
Note:
I want to stress that [ -z $VARIABLE ] is not enough, because you can have VARIABLE but it was not exported. That means that it is not an environment variable at all.
These are for cases where you know that the directory contains only regular files and that you want to process all non-hidden files. If that is not the case, use the approaches in 2.
All sed solutions in this answer assume GNU sed. If using FreeBSD or OS/X, replace -i with -i ''.
Non recursive, files in this directory only:
sed -i -- 's/foo/bar/g'*
perl -Ti -pe 's/foo/bar/g' ./*
(the perl one will fail for file names ending in | or space)).
Recursive, regular files (including hidden ones) in this and all subdirectories
find . -type f -exec sed -i 's/foo/bar/g' {} +
If you are using bash, bash having no support for glob qualifiers, you can't check for regular files: