6

When using the tcsh shell, how do I check if a folder does NOT exist?

I can check if it exists by

if ( -d /folder ) then

but I want the if statement to work for folders that don't exist.

1
  • What should happen if /folder is present, but a file? Or a link? Or some other type?
    – Twonky
    Jan 16, 2019 at 18:07

3 Answers 3

4

Just use

if (! -d /folder ) then
    #run some code here, if the folder does not exist
2

One thing you could do is use an else:

#!/usr/bin/tcsh

if ( -d folder) then 

else
        echo no
endif

Alternatively, you can do a negative check:

#!/usr/bin/tcsh

if (! -d folder) then 
   echo "No such folder"
0

The following script will check for the existence of the directory. If the directory does not exist, it is going to be created

#!/usr/bin/tcsh
if ( -e directory_name ) then
   echo 'Directory "directory_name" exists'
else
   mkdir directory_name
   echo 'Directory "directory_name" created'
endif

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .