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.

terdon
  • 98,183
  • 15
  • 197
  • 293
brain
  • 61
  • 1
  • 1
  • 2

3 Answers3

4

Just use

if (! -d /folder ) then
    #run some code here, if the folder does not exist
αғsнιη
  • 35,092
  • 41
  • 129
  • 192
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"
terdon
  • 98,183
  • 15
  • 197
  • 293
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
Igor
  • 101