simple check to see if a folder exists


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
simple check to see if a folder exists

Hi, I have cobbled together a simple script to create a Windows folder in a bunch of home folders on a mac server using the following code.

Code:
for i in /Volumes/student_data/studenthomefolders/*
do
u=`echo $i | cut -d/ -f5`
//if [ -d $i/Windows ]
//then
//echo "Folder already exists for "$u" Skipping"
//else
echo "Making Windows Dir for "$u
mkdir $i/Windows
echo "Changing Ownership of Windows Directory for "$u
chown $u $i/Windows
echo "Setting Mode for Windows Folder for "$u
chmod 700 $i/Windows
done

The commented out part is my attempt to add a check to see if that folder already exists and to skip the creation and manipulation if it does but my scripting knowledge is limited and that code returns an error.

If I run the script after creating a new user it currently runs through all of them, fails to create the existing ones but still chmods and chowns them, while this is not a massive problem I would like a neater script, anyone point me in the right direction.

Preferably it would silently skip existing ones and just report the new creations.

Many thanks in advance
# 2  
How about changing the logic to create a list of all directories that don't already have the Windows folder. Then use that list in your for loop to create them?
# 3  
Code:
for i in /Volumes/student_data/studenthomefolders/*
do
    u=`echo $i | cut -d/ -f5`
    if [ ! -d $i/Windows ]; then
        echo "Making Windows Dir for "$u
        mkdir $i/Windows
        echo "Changing Ownership of Windows Directory for "$u
        chown $u $i/Windows
        echo "Setting Mode for Windows Folder for "$u
        chmod 700 $i/Windows
    fi
done

# 4  
Many thanks, that diddn't quite work but altering it to

Code:
for i in /Volumes/student_data/studenthomefolders/*
do
    u=`echo $i | cut -d/ -f5`
    if [ -d $i/Windows ]; 
    then
    	echo $u" Windows folder exists" >/dev/null
    		else
        echo "Making Windows Dir for "$u
        mkdir $i/Windows
        echo "Changing Ownership of Windows Directory for "$u
        chown $u $i/Windows
        echo "Setting Mode for Windows Folder for "$u
        chmod 700 $i/Windows
    fi
done

worked like a charm, although I do get an error of too many arguments on line 4 but it still works so thanks. Is there a command that skips rather than a useless echo to null I can use?
# 5  
that's sorta odd...

If the directory doesn't exist, then do the stuff.

Pretty simple test.

What OS/shell version are you using?
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #192
Difficulty: Medium
Hello World in Python looks like this: printLine('Hello, world!')
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script which will check the target file and folder exists and copy it

Hi All, I am a beginner in this and trying to write a shell script in linux which will : 1. Ask for a file name and check if its exists. 2. If file exists only then it will ask for the new target folder, after entering target folder name it will check if it exists. 3. If target folder... (3 Replies)
Discussion started by: ashish_neekhra
3 Replies

2. Shell Programming and Scripting

File exists, but cannot be opened.How to check- whether it could be opened to read when it exists

Hi #Testing for file existence if ; then echo 'SCHOOL data is available for processing' else echo 'SCHOOL DATA IS NOT AVAILABLE FOR PROCESSING' : i wrote a script, where it begins by checking if file exists or not. If it exists, it truncates the database... (2 Replies)
Discussion started by: rxg
2 Replies

3. Shell Programming and Scripting

check if files exists

i writing a program that checks if any .txt files exist in the current directory if it does, then it lists the files... i have got everything right, except the validation part doesnt work! .... if then ls *.txt else echo "file not found" everytime it tells me file not found!! by... (3 Replies)
Discussion started by: bshell_1214
3 Replies

4. Shell Programming and Scripting

Check if remote folder exists

Hi, When trying to chk if a folder exists on remote server using the below command (got it from other thread in this forum) "ifvchr@s1.mrix.local '/cygdrive/d/shares/projects\ data\ load/test\ files/$SCPED_FILES$name$code'`]; then echo "Directory exists"; else echo "Directory... (0 Replies)
Discussion started by: funonnet
0 Replies

5. Shell Programming and Scripting

HOW TO CHECK ONLY .C FILES EXISTS OR NOT IN A FOLDER using IF in C shell script?

Hi friends.. I hav a problem.... I dont know how to check .c files exists r not in a folder using IF in C shell script actually i tried like this if(=~ *.c) even though some .c files or there in the current folder..it is not entering int o the if control statement...... (17 Replies)
Discussion started by: p.hemadrireddy
17 Replies

6. Shell Programming and Scripting

check if file exists in a mounted windows shared folder

hi, I posted a thread before on that subject, but with a wrong focus... here's my problem: I want to check if a file exists in a windows shared folder mounted using: sudo mount -t cifs -o username=xxx,password=xxx,uid=xxx,gid=xxx //192.168.0.92/public /media/92_shared I tried if ... (2 Replies)
Discussion started by: jul
2 Replies

7. Shell Programming and Scripting

How to check if a direcorty exists?

Hi Good people :D How do I check if a directory exists, if it does then carry on rest of the script, otherwise exit. ------------- cd $mainfolder/system1 #unzips files arrived in last 24 hrs into temp directory find * -mmin -1440 -exec unzip {} \; I'd like to check here if temp... (2 Replies)
Discussion started by: SunnyK
2 Replies

8. Shell Programming and Scripting

check if directory exists

Hi, I need to prompt for a response from a user to enter a path read dest_dir?"Please Enter Directory :" How do I do this until a valid directory is entered by the user. I can use to check the existence of the directory. However when I try the following I cannot get it to work. while ... (2 Replies)
Discussion started by: jerardfjay
2 Replies

9. Shell Programming and Scripting

Check Remote Folder Exists

Hi, I want to sftp some files to a remote directory. Before transferring files i want to check whether the required folder exists. If so copy the files to that folder, else create the folder and copy the files. Thanks in adv (1 Reply)
Discussion started by: borncrazy
1 Replies

10. UNIX for Dummies Questions & Answers

Folder Exists

Hi, How do we check whether a folder exists already using unix script? My requirement is to check whether a folder already exists, if not create a folder else return a warning. Can anyone help me with this requirement Thanks (5 Replies)
Discussion started by: borncrazy
5 Replies

Featured Tech Videos