LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-09-2008, 01:10 PM   #1
nanxy
LQ Newbie
 
Registered: Jul 2008
Posts: 16

Rep: Reputation: 0
to find if a file exist using shell script


Hi~ guys, i am new to writing script. I have a question about that.
Say if I have a directory and have several files in it, I want to use the script to find out if a certain file exit here and if this file does exit. i will do something to the file. How can I define the 'exist' in a script?
Thank you
 
Old 09-09-2008, 01:23 PM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,192

Rep: Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469
Quote:
Originally Posted by nanxy View Post
Hi~ guys, i am new to writing script. I have a question about that.
Say if I have a directory and have several files in it, I want to use the script to find out if a certain file exit here and if this file does exit. i will do something to the file. How can I define the 'exist' in a script?
Thank you
Lots of info on Google about this, and basic shell scripting.

Code:
#!/bin/bash
FILE=$1

if [ -f $FILE ];
then
echo "File $FILE exists"
else
echo "File $FILE does not exists"
fi
 
Old 09-09-2008, 02:14 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
-f is a test to see if a file exists and is a regular file. From the bash manual page:
Quote:
-a file
True if file exists.
-b file
True if file exists and is a block special file.
-c file
True if file exists and is a character special file.
-d file
True if file exists and is a directory.
-e file
True if file exists.
-f file
True if file exists and is a regular file.
-g file
True if file exists and is set-group-id.
-h file
True if file exists and is a symbolic link.
-k file
True if file exists and its ‘‘sticky’’ bit is set.
-p file
True if file exists and is a named pipe (FIFO).
-r file
True if file exists and is readable.
-s file
True if file exists and has a size greater than zero.
-t fd True if file descriptor fd is open and refers to a terminal.
-u file
True if file exists and its set-user-id bit is set.
-w file
True if file exists and is writable.
-x file
True if file exists and is executable.
-O file
True if file exists and is owned by the effective user id.
-G file
True if file exists and is owned by the effective group id.
-L file
True if file exists and is a symbolic link.
-S file
True if file exists and is a socket.
-N file
True if file exists and has been modified since it was last read.
file1 -nt file2
True if file1 is newer (according to modification date) than file2, or if file1 exists and
file2 does not.
file1 -ot file2
True if file1 is older than file2, or if file2 exists and file1 does not.
file1 -ef file2
True if file1 and file2 refer to the same device and inode numbers.
 
Old 09-09-2008, 03:36 PM   #4
nanxy
LQ Newbie
 
Registered: Jul 2008
Posts: 16

Original Poster
Rep: Reputation: 0
Thank u very much. But is there a way say that if a file does not exit?
 
Old 09-09-2008, 03:40 PM   #5
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,192

Rep: Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469Reputation: 2469
Quote:
Originally Posted by nanxy View Post
Thank u very much. But is there a way say that if a file does not exit?
Read my original post. I pasted a sample script. which takes two actions, one if the file is there, the other if it isn't. You can only check if something DOES exist, but you can act on the condition of what you find.
 
Old 09-10-2008, 03:37 AM   #6
btiffin
LQ Newbie
 
Registered: Sep 2008
Posts: 1

Rep: Reputation: 1
Quote:
Originally Posted by nanxy View Post
Thank u very much. But is there a way say that if a file does not exit?
As TBOne stated, or

Code:
if ! [ -a filename ]; then echo "not exists"; fi
You always have to be careful when you "not" a test. -f would be not exists OR not regular file etc... So ! -f is true for directories.

Cheers,
Brian

P.S. Excuse the interruption. It's my first post and I want to get rid of the "you haven't posted anything" nag box, so I thought I'd start with this one.
 
1 members found this post helpful.
Old 09-11-2008, 09:36 AM   #7
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian
Posts: 1,265

Rep: Reputation: 98
Thumbs up This might give you some more idea

http://www.troubleshooters.com/linux...criptFileTests

TEST MEANING
[ -b $file ] True if file exists and is block special.
[ -c $file ] True if file exists and is character special.
[ -d $file ] True if file exists and is a directory.
[ -e $file ] True if file exists.
[ -f $file ] True if file exists and is a regular file.
[ -g $file ] True if file exists and is set-group-id.
[ -k $file ] True if file has its ``sticky'' bit set.
[ -L $file ] True if file exists and is a symbolic link.
[ -p $file ] True if file exists and is a named pipe.
[ -r $file ] True if file exists and is readable.
[ -s $file ] True if file exists and has a size greater than zero.
[ -S $file ] True if file exists and is a socket.
[ -t $fd ] True if fd is opened on a terminal.
[ -u $file ] True if file exists and its set-user-id bit is set.
[ -w $file ] True if file exists and is writable.
[ -x $file ] True if file exists and is executable.
[ -O $file ] True if file exists and is owned by the effective user id.
[ -G $file ] True if file exists and is owned by the effective group id.

Regards,
VIKAS
 
Old 09-16-2013, 10:20 PM   #8
free6d1823
LQ Newbie
 
Registered: Dec 2004
Location: Taiwan
Posts: 1
Blog Entries: 6

Rep: Reputation: 0
Quote:
Originally Posted by btiffin View Post
As TBOne stated, or

Code:
if ! [ -a filename ]; then echo "not exists"; fi
I found this also works:
Code:
if [ ! -f $FILE ];
then
echo "File $FILE does not exist"
else
echo "File $FILE exists"
fi
 
Old 09-16-2013, 10:30 PM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,528
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
If you use the [ ... ] form of testing you need to quote the variable in case it contains spaces or similar:
Code:
c@CW8:/tmp$ file='file name with spaces'
c@CW8:/tmp$ touch "$file"
c@CW8:/tmp$ if [ -f $file ]; then echo exists; else echo does not exist; fi 
bash: [: too many arguments
does not exist
c@CW8:/tmp$ if [ -f "$file" ]; then echo exists; else echo does not exist; fi 
exists
The [[ ... ]] form is more robust and hence recommended (http://mywiki.wooledge.org/BashFAQ/031):
Code:
c@CW8:/tmp$ if [[ -f $file ]]; then echo exists; else echo does not exist; fi  
exists
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to find the difference betwwn two file and place the difference to other kittunot4u Linux - General 3 07-19-2010 04:26 AM
Need to find if a matching file exist from a list of possible file names wit_273 Linux - General 5 10-25-2007 09:47 AM
Shell script: how to find a file containing a string guarriman General 2 08-27-2007 03:39 AM
How to find out whether a process is dead or alive from within a shell script file walwali Linux - Software 2 12-24-2005 01:39 PM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM


All times are GMT -5. The time now is 10:28 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration