Unix/Linux Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

how to check if the file exist or not?

UNIX for Dummies Questions & Answers


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-27-2002
gusla gusla is offline
Registered User
 
Join Date: Feb 2002
Last Activity: 23 November 2004, 12:49 AM EST
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Data how to check if the file exist or not?

say i would like to check if the file is existed before i use rm command. How can i do it?

i know if i can use find, but i would like to have a good interface (in a shell script)



thks
Sponsored Links
    #2  
Old Unix and Linux 03-27-2002
LivinFree's Unix or Linux Image
LivinFree LivinFree is offline Forum Advisor  
Goober Extraordinaire
 
Join Date: Jul 2001
Last Activity: 16 June 2011, 4:50 PM EDT
Location: Portland, OR, USA
Posts: 1,626
Thanks: 2
Thanked 15 Times in 13 Posts
See the man page for test.

if [ -s $file_name ]; then
echo Yay
else
echo Boo
fi

That will Yay if the file exists, and has a size greater than zero, and Boo if the file does not exist, or has a 0 length.
The Following User Says Thank You to LivinFree For This Useful Post:
vel4ever (01-07-2012)
Sponsored Links
    #3  
Old Unix and Linux 03-27-2002
krylin krylin is offline
Registered User
 
Join Date: Mar 2002
Last Activity: 8 June 2003, 5:31 PM EDT
Location: Germany
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
You should try the following code:

test -e $file_name && echo "file exists"
    #4  
Old Unix and Linux 03-27-2002
gusla gusla is offline
Registered User
 
Join Date: Feb 2002
Last Activity: 23 November 2004, 12:49 AM EST
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
thk

u both
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
check file exist before execution guddu_12 Shell Programming and Scripting 7 04-25-2012 10:10 AM
how to check file exist in a directory or not sravan008 Shell Programming and Scripting 5 07-21-2011 01:57 AM
Check if file exist palak08 Shell Programming and Scripting 4 12-30-2010 04:19 AM
Check if file exist palak08 Shell Programming and Scripting 1 12-05-2010 07:56 PM
Check if file exist gksenthilkumar Shell Programming and Scripting 3 05-11-2009 07:11 PM



All times are GMT -4. The time now is 10:46 PM.