Find & If commands together


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find & If commands together
# 1  
Old 04-02-2009
Find & If commands together

Hi to everybody!!

I have a (simple) question but i am newbie with unix and so i need a little help...I am writing a bash script file and i want to put inside this:

i have this command " find /usr/bin -name bzip2 -print " that i want to put it in a "if" statement and when it returns true the "programm" to continue otherwise to print a message "the bzip2 file wasn't found".

anybody has any idea?

thanks in advance!!Smilie
# 2  
Old 04-02-2009
if all you want is to do something if the file exists then:

Code:
if [ -e /path/to/file ]; then
        echo "Found it"
        #Do something here

else
        echo "Not Found"
        #Do something else
fi

# 3  
Old 04-02-2009
if i don't know where is the file and i have to search for it under a folder how can i do it?

and second what is the meaning of the "-e" expression?

Thanks for your quick reply! Smilie
# 4  
Old 04-02-2009
Code:
-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. 
-z string = True if the length of the string is 0. 
-n string = True if the length of the string is non-zero.

# 5  
Old 04-02-2009
Quote:
Originally Posted by orestis7
if i don't know where is the file and i have to search for it under a folder how can i do it?

about this any ideas?Smilie
# 6  
Old 04-02-2009
Try this:

Code:
SEARCHPATH="/usr/bin"
FILE="bzip2"
FINDIT=`find /usr/bin -name $FILE -print`

if [ ${#FINDIT} -gt 0  ]; then

        echo "Found it: $FINDIT"
else
        echo "Not Found"
fi


edit:Had to change it.
# 7  
Old 04-02-2009
The statement ikon provided will also return true if find returns an error on reading a subdirectory. You need to pipe the errors to null to avoid that possibility. I would also add that find has the -exec option so depending on what you want to do when you find the file you may be able to handle it within the find command. One more thing 'bzip2' has no file extention, so I would recommend restricting find to only files '-type f' just in case you run across a directory with that name.
Code:
#FINDIT=`find /usr/bin -name $FILE -print`
FINDIT=`find /usr/bin -type f -name $FILE -print 2>/dev/null`

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find a file with a specific pattern for current sysdate & upon find email the details?

I need assistance with following requirement, I am new to Unix. I want to do the following task but stuck with file creation date(sysdate) Following is the requirement I need to create a script that will read the abc/xyz/klm folder and look for *.err files for that day’s date and then send an... (4 Replies)
Discussion started by: PreetArul
4 Replies

2. Solaris

Restricting commands & access

Dear all, I am administering a DC environment of over 100+ Solaris servers used by various teams including Databases. Every user created on the node belonging to databases is assigned group staff(10) . I want that all users belonging to staff should NOT be able to execute certain system... (6 Replies)
Discussion started by: Junaid Subhani
6 Replies

3. Shell Programming and Scripting

Grep commands & format

I have these grep commands and need to put them next each other (in horizontal layout). cat /tmp/dsmc.out |grep Done cat /tmp/dsmc.out |grep "Elapsed processing time:" cat /tmp/dsmc.out |grep "Client date/time:" cat /tmp/dsmc.out |grep "Total number of bytes transferred:" so that it... (6 Replies)
Discussion started by: Daniel Gate
6 Replies

4. Shell Programming and Scripting

Using Grep & find & while read line in a script

Hello people! I would like to create one script following this stage I have one directory with 100 files File001 File002 ... File100 (This is the format of content of the 100 files) 2012/03/10 12:56:50:221875936 1292800448912 12345 0x00 0x04 0 then I have one... (0 Replies)
Discussion started by: Abv_mx81
0 Replies

5. Shell Programming and Scripting

How to use grep & find command to find references to a particular file

Hi all , I'm new to unix I have a checked project , there exists a file called xxx.config . now my task is to find all the files in the checked out project which references to this xxx.config file. how do i use grep or find command . (2 Replies)
Discussion started by: Gangam
2 Replies

6. Shell Programming and Scripting

Append && echo "success" to all commands

I am learning to build from SVN and other tools, with a lot of copying and pasting from forums. I like to append && echo "success" to all commands so that I can see at a glance if things went all right. Is there a way that I can have the bash shell append this to all commands? Thanks! (5 Replies)
Discussion started by: dotancohen
5 Replies

7. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

8. UNIX for Dummies Questions & Answers

help with find & grep commands

Folks; First about find: when i run this: find . -name '*log*' -mtime +10 -print | sed 's+^\./++;s+/.*++' | sort -u i got list of log files but also get a directories (although directory names doesn't have "log" in it). How can i exclude the directory from the output of this find command? ... (2 Replies)
Discussion started by: moe2266
2 Replies

9. SCO

compress & cpio commands

Our End of Day backup routine uses following script. start End-of-day compress $BASE TO /home/compdir write /home/compdir to DATTAPE end where $BASE=/home2/Rev83 DATATAPE=/dev/rmt/ctape1 write=cpio (not sure about parameters) since I'm new to UNIX, i dont know how to restore data... (1 Reply)
Discussion started by: tayyabq8
1 Replies
Login or Register to Ask a Question