Go Job Hunting at the LQ Job Marketplace
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 08-06-2003, 03:26 AM   #1
Registered: Jul 2003
Distribution: HP-UX
Posts: 35

Rep: Reputation: 15
Search for a file using shell script


I am new to shell scripts and wrote a very basic script as follows:

#bash script
#Script to search for a file given its filename as a parameter.

if($1 == 0)
print ' No arguments specified ';
find / '!' -type d -name $1 -print0

# End of Script

However I am getting errors when I try to execute the above

Also I am getting "permission denied" message on lots of

How can I write a script that truly searches the entire drive for
the filename specified without any access problems?

Any help is appreciated.
Old 08-06-2003, 05:50 AM   #2
Registered: Jul 2003
Location: Geneva, Switzerland
Distribution: Debian 3.1, SLC3 (based on RHEL)
Posts: 84

Rep: Reputation: 15
>#bash script
instead of this, you should put:
#!/bin/sh (for bash shell)
>#Script to search for a file given its filename as a parameter.
>if($1 == 0)
if [$1 == 0]; then
>print ' No arguments specified ';
echo "No arguments specified"
exit 0
>find / '!' -type d -name $1 -print0
exit 0
># End of Script
This should work (I hope!)
You can also look at this web page for bash scripts:

and for your permission denied problems, you can only avoid these problems by being root while executing your script, or change the permissions on all folders! There is no way out!

Hope this help! I am not an expert myself, but I am trying hard!

Old 08-06-2003, 06:05 AM   #3
Senior Member
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 46
You can loose the error message if you wish by redirecting STDERR to null in your shell script. So you would use find / '!' -type d -name $1 -print0 2> /dev/null

You say that your script will search for files yeah? Uhm... No... You appear to be searching for directories (-type d) if you want to look for just files then you want -type f




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
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 01:13 AM
Script to search and replace in text file - kinda... jeffreybluml Programming 45 11-07-2004 06:37 PM
Search and replace text in file using shell script? matthurne Linux - Software 2 11-02-2004 11:11 AM
Help with file search and destroy script flippy Programming 5 07-25-2003 05:29 PM
Shell-script: search item in any order macone Programming 3 08-13-2002 11:44 AM

All times are GMT -5. The time now is 07:34 PM.

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