The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
check if remote file exists hcclnoodles Shell Programming and Scripting 2 08-27-2008 05:53 PM
Check if a file exists with certain prefix raoscb Shell Programming and Scripting 11 08-20-2008 08:13 AM
check if file exists on remote system ? hcclnoodles Shell Programming and Scripting 2 10-26-2006 04:08 AM
check if exists a .ZIP file and unzip it using ftp DebianJ Shell Programming and Scripting 1 05-05-2005 04:46 PM
perl ftp check file exists methos Shell Programming and Scripting 2 06-18-2003 08:21 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-19-2008
Jazmania Jazmania is offline
Registered User
  
 

Join Date: Feb 2007
Location: Ireland
Posts: 43
How to check if a file exists using the if statement

Hi,
I'm trying to write a bit of code that will check if a file exists and then archives the file

Im trying to use the following if statement without success..

if [ -f = "filename" ]
then
mv filename archive/filename
else
echo "no filename exists"
fi

Should the file name be in quotes? My code's not finding the file, it alway moves on to the else section...

EDIT* I'm scripting in KSH..

Last edited by Jazmania; 09-19-2008 at 12:18 PM..
  #2 (permalink)  
Old 09-19-2008
avronius avronius is offline VIP Member  
VIP Member
  
 

Join Date: Apr 2008
Location: Calgary
Posts: 305
From your code remove the "=" in the test segment
Code:
if [ -f "filename" ]
then
mv filename archive/filename
else
echo "no filename exists"
fi
  #3 (permalink)  
Old 09-19-2008
Ikon's Avatar
Ikon Ikon is offline Forum Advisor  
Registered User
  
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 669
Here are more shell test conditions:
Code:
The tests below are test conditions provided by the shell: 

-b file = True if the file exists and is block special file. 
-c file = True if the file exists and is character special file. 
-d file = True if the file exists and is a directory. 
-e file = True if the file exists. 
-f file = True if the file exists and is a regular file 
-g file = True if the file exists and the set-group-id bit is set. 
-k file = True if the files' "sticky" bit is set. 
-L file = True if the file exists and is a symbolic link. 
-p file = True if the file exists and is a named pipe. 
-r file = True if the file exists and is readable. 
-s file = True if the file exists and its size is greater than zero. 
-s file = True if the file exists and is a socket. 
-t fd = True if the file descriptor is opened on a terminal. 
-u file = True if the file exists and its set-user-id bit is set. 
-w file = True if the file exists and is writable. 
-x file = True if the file exists and is executable. 
-O file = True if the file exists and is owned by the effective user id. 
-G file = True if the file exists and is owned by the effective group id. 
file1 –nt file2 = True if file1 is newer, by modification date, than file2. 
file1 ot file2 = True if file1 is older than file2. 
file1 ef file2 = True if file1 and file2 have the same device and inode numbers. 
-z string = True if the length of the string is 0. 
-n string = True if the length of the string is non-zero. 
string1 = string2 = True if the strings are equal. 
string1 != string2 = True if the strings are not equal. 
!expr = True if the expr evaluates to false. 
expr1 –a expr2 = True if both expr1 and expr2 are true. 
expr1 –o expr2 = True is either expr1 or expr2 is true.
  #4 (permalink)  
Old 09-19-2008
Jazmania Jazmania is offline
Registered User
  
 

Join Date: Feb 2007
Location: Ireland
Posts: 43
Thanks guys...

I hate it when something so simple throws off the whole thing....
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:28 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0