Check if file exists, and create file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Check if file exists, and create file
# 1  
Old 11-19-2009
Check if file exists, and create file

hi i have 2 questions:
1. how can i check if file exist in vi?
i tryed to do:
if [ -s $file ]; then
echo file exist

but i get from the compiler
if: Expression Syntax.

2. how can i create file in vi?
when im using cat - so the script stop and wait for me to write somthing into the new file
when im using touch - the file created, but i get the messege :
touch: file arguments missing
Try `touch --help' for more information.

???
# 2  
Old 11-19-2009
Sounds to me like the variable "file" is not set.

If "file" is literally the name of the filename then you don't need the $ sign.

Code:
if [ -s file ] ...

touch file

Otherwise

Code:
file=myFile

if [ -s $file ] ...

touch $file

# 3  
Old 11-22-2009
I think the proper construct is -e, however I am a novice shell programmer

Code:
#!/bin/bash

myfile="/path/to/my/file"

if [[ -e $myfile ]] 

    then /bin/echo "$myfile already exists"

    else /usr/bin/touch $myfile

fi

done

exit 0

The -e switch can only handle one file at a time, so if you want, you can build an array of file paths and loop it for checks. So, lets say you wanted to check for multiple files...

Code:
#!/bin/bash

myfile=(
           /path/to/my/file1
           /path/to/my/file2
           /path/to/my/file3
           /path/to/my/file4
           )

for file in "${myfile[@]}" ; do

if [[ -e $myfile ]] 

    then /bin/echo "$myfile already exists"

    else /usr/bin/touch $myfile

fi

done

exit 0

This will allow you to maintain a list of files to check, and if the file doesn't exist it will create it. Also, if this is run as root, it will create the file as root as the owner. So, you may have to add another part if you don't want root to own the file.
This User Gave Thanks to tlarkin For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if file exists

I need to check whether a file exists and has been changed. The file should contain a specific string. The file should also have been changed within the last ten seconds. How do I do that? (3 Replies)
Discussion started by: locoroco
3 Replies

2. Shell Programming and Scripting

To check if file exists

Hi, I have the below code written. However I am not getting the desired output I am checking if the particular path has file in it. #!/bin/bash ls -l /IRS2/IRS2_ODI/INFILE/*LS* 1>/dev/null 2>/dev/null if then echo $? echo "File Exists" fi ... (3 Replies)
Discussion started by: Shanmugapriya D
3 Replies

3. Shell Programming and Scripting

See if file exists and if not create it

Hi, I am using an expect script to roll out an ssh key to multiple servers. I have a problem as on some of them the authorized_keys file exists and on some it doesn't. Where it exists I need to >> the key in and where it doesn't I need to create the file then sftp the file over. So I need some... (2 Replies)
Discussion started by: Grueben
2 Replies

4. Shell Programming and Scripting

how to check if a directory exists or not.if not need to create it

Hi, I am using solaris 10 OS and bash shell.just checking for small code snippet which follows below. /export/home/vomappservers/spa/common/5.0 /export/home/vomappservers/spa/common/scripts /export/home/vomappservers/spa/tools /export/home/vomappservers/spa/scm5.0/SCCS... (5 Replies)
Discussion started by: muraliinfy04
5 Replies

5. Shell Programming and Scripting

how to check to see if a file exists?

I want to write a script to see if various files exist. What I want to do is have the script search in various directories if a file exist, and if not, then output something like "/path/file does not exist". I don't actually know of how to check and see if a file exists or not. What I have in mind... (2 Replies)
Discussion started by: astropi
2 Replies

6. Shell Programming and Scripting

Script to check for the file existence, if file exists it should echo the no of modified days

Hi, I am looking for a shell script with the following. 1. It should check whether a particular file exists in a location #!/bin/sh if ; then echo "xxx.txt File Exists" else echo "File Not Found" fi 2. If file exists, it should check for the modified date and run a command... (2 Replies)
Discussion started by: karthikeyan_mac
2 Replies

7. Shell Programming and Scripting

Newbie.. Find if a file exists and open, if not create the desired file..

Hey all, I'm brand new to script writing, I'm wanting to make a script that will ask for a file and then retrieve that file if it exists, and if it doesn't exist, create the file with the desired name, and I'm completely stuck.. so far.. #! bin/bash echo "Enter desired file" read "$file" if ... (5 Replies)
Discussion started by: Byrang
5 Replies

8. Shell Programming and Scripting

check a file is exists in multiple tar file?

I have list of ‘tar' format files in a folder. I want to check a particular file is exists in the tar files. Currently I am using the below command to check the scenario. tar -tvf first.tar | grep ‘myfile.txt' The command able to searched a single ‘tar' file only. But I want to search... (7 Replies)
Discussion started by: k_manimuthu
7 Replies

9. Shell Programming and Scripting

Check if file exists, create new file?

Hi everyone, I'm brand new to shell scripts (and UNIX in general) and I've been able to figure everything out except this... I want to write a script that will take a filename as an argument, append the date to it (IE Dec 24 2008) and make sure it doesn't already exist, if so, add an integer to... (4 Replies)
Discussion started by: solarnoise
4 Replies

10. Shell Programming and Scripting

Check File Exists and compare to previous day file script

We have data files that are ftp'd every morning to a SUN server. The file names are exactly the same except for that each has the date included in its name. I have to write script to do 2 things: STEP 1) Verify that the file arrived in morning. STEP 2) Compare the file size of the current... (3 Replies)
Discussion started by: rbknisely
3 Replies
Login or Register to Ask a Question