Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a directory exists and is accessible

Tags:

linux

bash

unix

ksh

I want to check if a directory exists and it has access rights; if it does, then perform the tasks. This is the code I wrote, which might not have proper syntax.

Can you help me to correct it?

dir_test=/data/abc/xyz
if (test -d $dir_test & test –x $dir_test -eq 0);
 then
cd $dir_test
fi

I believe this can also be written like this.

dir_test=/data/abc/xyz
test -d $dir_test
if [ $? -eq 0 ];
then
test –x $dir_test
if [ $? -eq 0 ];
then
cd $dir_test
fi
fi

How can we write this more efficiently?

like image 977
Rahul sawant Avatar asked Mar 24 '14 15:03

Rahul sawant


People also ask

How can I tell if a directory is readable?

canRead() is used to check if a file or directory is readable in Java. This method returns true if the file specified by the abstract path name can be read by an application and false otherwise.

How do I check if a directory exists in path?

The Test-Path Cmdlet $Folder = 'C:\Windows' "Test to see if folder [$Folder] exists" if (Test-Path -Path $Folder) { "Path exists!" } else { "Path doesn't exist." } This is similar to the -d $filepath operator for IF statements in Bash. True is returned if $filepath exists, otherwise False is returned.

How do you check if a folder exists and if not create it?

You can use os. path. exists('<folder_path>') to check folder exists or not.

Does mkdir check if directory exists?

mkdir WILL give you an error if the directory already exists. mkdir -p WILL NOT give you an error if the directory already exists. Also, the directory will remain untouched i.e. the contents are preserved as they were.


2 Answers

The best way to write the original test-based solution would be

if test -d "$dir_test" && test –x "$dir_test";
then
    cd $dir_test
fi

although what will you do if the test fails and you don't change directories? The remainder of the script will probably not work as expected.

You can shorten this by using the [ synonym for test:

if [ -d "$dir_test" ] && [ -x "$dir_test" ]; then

or you can use the conditional command provided by bash:

if [[ -d "$dir_test" && -x "$dir_test" ]]; then

The best solution, since you are going to change directories if the tests succeed, is to simply try it, and abort if it fails:

cd "$dir_test" || {
  # Take the appropriate action; one option is to just exit with
  # an error.
  exit 1
}
like image 197
chepner Avatar answered Sep 21 '22 12:09

chepner


dir_test=/data/abc/xyz
if (test -d $dir_test & test –x $dir_test -eq 0); # This is wrong. The `-eq 0` part will result in `test: too many arguments`. The subshell (parens) is also unnecessary and expensive.
 then
cd $dir_test
fi

cd can tell you if a directory is accessible. Just do

cd "$dir_test" || exit 1;

Even if you do decide to use test first, for some reason, you should still check the exit status of cd, lest you have a race condition.

like image 40
kojiro Avatar answered Sep 22 '22 12:09

kojiro