nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Test Linux file exists

This is a discussion on Test Linux file exists within the Getting started tutorials forums, part of the Linux Getting Started category; You can easily test if a file exists under Linux operating system using test command. Display a message if file ...


Go Back   nixCraft Linux Forum > Linux Getting Started > Getting started tutorials

Linux answers from nixCraft.


Getting started tutorials So much to read, so little time! If that is your problem, we have solution. Read our FAQ and tutorials to help you cut through the clutter of information overload. Only members of "contributors" group can post new tutorials. Other members can just reply to thread.

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 16-11-2008, 09:22 PM
raj's Avatar
raj raj is offline
Senior Member
User
 
Join Date: Jun 2005
Location: Hyderabad
OS: Fedora, Debian Linux
Posts: 307
Thanks: 42
Thanked 8 Times in 8 Posts
Rep Power: 6
raj will become famous soon enough raj will become famous soon enough
Default Test Linux file exists

You can easily test if a file exists under Linux operating system using test command.

Display a message if file /etc/passwd exists:
Code:
test -f /etc/passwd && echo 'File exists'
Display a message if file /etc/passwd2 does not exists:
Code:
test -f /etc/passwd2 && echo 'File exists' || echo 'File not found'
You can also use [ ]:
Code:
[ -f /path/file.txt ] && echo 'File exists' || echo 'File not found'
Here is if command example:
Code:
if [ -f /etc/passwd ]
then
     echo 'Adding user to database...'
else
     echo 'File not found!'
fi
More info:

* Find out if file exists with conditional expressions

* man test
__________________
Raj
Linux rulz.
I have never turned back in my life ; I shall not do so today.. haha
Reply With Quote
  #2 (permalink)  
Old 18-11-2008, 09:38 PM
Junior Member
User
 
Join Date: Nov 2008
OS: Debian
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
thePITman is on a distinguished road
Default

Urgent!

I am having an issue with this. We are switching from UNIX to Linux, so I am going through programs making sure they work on Linux. I am having the following problem:

I run a .prog file that, in turn, runs a PL/SQL program that creates one or more output files that all begin with the same 7 characters, and have similar to the following format:

411n9n_xxx_xxx_timestamp

Once back in the .prog file, my code is as follows:
(I have already changed single brackets to double bracket because of syntax error)

Code:
if [[ -f ${EXPORTDIR}/411n9n* ]]
then
   xxx
fi
This does not work, and the IF statement is not entered into even when there are files located in the folder that meet the requirements. However, if I change the expression to an exact filename (without the * wildcard) it works fine. Is there some kind of limitation on the wildcard in this situation?

Thank you!

Last edited by thePITman; 18-11-2008 at 09:45 PM.
Reply With Quote
  #3 (permalink)  
Old 18-11-2008, 10:52 PM
Junior Member
User
 
Join Date: Nov 2008
OS: Debian
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
thePITman is on a distinguished road
Default

Arrgggh!!! It works now!

The whole problem was the double brackets. However, when it was just single brackets when I first started testing, it gave me an error about "too many" or "not enough arguments" but the error went away when I made it double brackets instead of just one. So the whole time there was nothing wrong with my original code!

What a waste of time. Haha!
Reply With Quote
  #4 (permalink)  
Old 26-11-2008, 06:48 PM
Junior Member
User
 
Join Date: Nov 2008
OS: Debian
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
nbdwt73 is on a distinguished road
Default

I have a similar issue - We have a Windows based database that is backed up to a linux server (Debian). The file names are similar but with a date suffix. What I need to do is confirm that a particular backup file - or set of files - is present (meaning backup completed) and then process. Script follows:

#!/bin/bash
FILE="/media/Backup/SYS_0*"
if [ -f $FILE ]; then
rm -f /media/Backup/SYS_3*
rename -f 's/SYS_2/SYS_3/' /media/Backup/SYS*
rename -f 's/SYS_1/SYS_2/' /media/Backup/SYS*
rename -f 's/SYS_0/SYS_1/' /media/Backup/SYS*
fi

The problem is that multiple SYS_0... files can exist. The script works fine if only one exists. What am I doing wrong? I have tried thePITman suggestion above but it does not work for me - if [[ -f $FILE ]]...

Thanks for any help.
Reply With Quote
  #5 (permalink)  
Old 27-11-2008, 04:27 PM
Junior Member
User
 
Join Date: Nov 2008
OS: Debian
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
nbdwt73 is on a distinguished road
Default

Got it... The instruction that works for me is:

if [ ! -f "$FILE" ]; then ...
Reply With Quote
Reply

Tags
files , shell scripting , test


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 On
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Linux convert doc file to pdf file suneel243 CentOS / RHEL / Fedora 0 16-10-2008 10:27 AM
Find Unix Linux File / Directory by date And Then Copy / Move File asim.mcp CentOS / RHEL / Fedora 1 10-08-2008 03:30 AM
Check file exists on Remote Server sanjus Shell scripting 3 12-06-2008 06:05 AM
script on how to check if the file exists warren Shell scripting 4 14-04-2006 05:51 PM
freebsd ifconfig alias up ioctl siocaifaddr file exists erro ZigZag All about FreeBSD/OpenBSD/NetBSD 1 23-12-2005 04:49 PM


All times are GMT +5.5. The time now is 12:54 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38