LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 09-28-2012, 08:49 PM   #1
z1078516
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Rep: Reputation: Disabled
Unhappy How to create a script using the "find" command


Shown below is an example of how I use the Linux find command. It searches all folders below to show where the file named test may reside.

find . -name test -print

Shown here is a typical response I get. In this case, the find command has found the file named test down in the tmp directory.

./tmp/test

To minimize typing, I would like to create an alias or script using the find command string above. I invision something like:

fnd test

The file name test would be a variable where I could put any file name.

Thanks for any help
 
Old 09-28-2012, 09:14 PM   #2
sackboy
LQ Newbie
 
Registered: Sep 2012
Posts: 20

Rep: Reputation: Disabled
Hi,

Is your script only going to look in the current directory? Will you always be looking for all types?

If yes, your script (fnd):

Code:
#!/bin/bash

find . -name $1 -print
 
Old 09-28-2012, 09:32 PM   #3
Rupadhya
Member
 
Registered: Sep 2012
Location: Hoffman Estates, IL
Distribution: Fedora 20
Posts: 167

Rep: Reputation: Disabled
You could try something like this...

Code:
#! /bin/bash
find / -name $1 -print 2>/dev/null
## the / starts the find at the root of your filesystem and
## the 2>/dev/null will hide any permission errors.

save it as a script like myFind.sh
chmod +x myFind.sh #to give it execute
./myFind.sh {filename} to run it..

## I haven't tested this, but it should work. It might be slow,
## depending how big your filesystem is..
## - Raj

Last edited by Rupadhya; 09-28-2012 at 09:33 PM. Reason: Forgot code block
 
Old 09-28-2012, 09:40 PM   #4
z1078516
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Sackboy

Thanks for your reply. I thank that is exactly what I want.
However, I am not sure what to do from here.

I created a file called fnd which contains the code below:

#!/bin/bash
find . -name $1 -print

I created a folder called scripts in my home directory
and put the file called fnd at that location.

When I type fnd, I get the response:

fnd: Command not found


Thanks for any additional help.
 
Old 09-29-2012, 12:30 AM   #5
Rupadhya
Member
 
Registered: Sep 2012
Location: Hoffman Estates, IL
Distribution: Fedora 20
Posts: 167

Rep: Reputation: Disabled
Try this..
Code:
chmod +x ~/scripts/fnd
~/scripts/fnd {blah}
The ~ indicates your home directory and the chmod +x modifies a regular file into an executable file.

Alternatively, you can put your scripts directory on the path with this..

export PATH=$PATH:~/scripts

- Raj

Last edited by Rupadhya; 09-29-2012 at 12:35 AM. Reason: alternative
 
Old 09-29-2012, 12:41 AM   #6
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

Rep: Reputation: 42
simply create an alias

script

Quote:
#!/bin/bash

find / -name $1 -print
alias fnd='sh test.sh'


then at the command line , just do

Quote:
fnd test

Last edited by cbtshare; 09-29-2012 at 12:54 AM.
 
Old 09-30-2012, 09:46 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
You need to write either a shell script, as posted above, or a shell function, which is pretty much the same thing, except that it resides inside the current shell environment rather than a separate file.

To set it up as a function, put this in your .bashrc file.
Code:
fnd(){
	find / -name "$1" -print 
}
Once the terminal is restarted (or .bashrc reloaded), then "fnd <searchterm>" should work (be sure to quote terms with spaces or other reserved characters).

You could even expand it to include the starting directory.

Code:
fnd(){
	find "$1" -name "$2" -print 
}

fnd "<startdir>" "<searchterm>"
As for the alias here:

Code:
alias fnd='sh test.sh'
This is not the correct way to launch the script. by specifying sh as the interpreter in the alias you are telling it to ignore the original /bin/bash shebang entirely. Better to just make it executable, and run it directly. Also don't forget that you need the full path to the script.

Code:
alias fnd='/path/to/script.sh'

Name the script properly, make it globally executable, and put it in a location in your PATH, and you can even skip the alias entirely.

As root, assuming /usr/local/bin is in your PATH:
Code:
mv test.sh /usr/local/bin/fnd
chmod a+x /usr/local/bin/fnd
PS: You need to double-quote the $1 parameter too, as I did above, so that it can properly handle shell-reserved characters.

Last edited by David the H.; 09-30-2012 at 09:51 AM. Reason: edits after re-reading thread
 
Old 09-30-2012, 10:54 AM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
No need to write any script for this, you can just use a simple alias.

-print is the default behavior, so you can leave it off. What you're left with is:
Code:
alias fnd='find . -name'
And that's it.

The advantage of using a straight alias instead of a script is that you can throw extra flags on there as well, such as
Code:
fnd test -type f

Last edited by suicidaleggroll; 09-30-2012 at 11:37 AM. Reason: Typo
 
Old 09-30-2012, 11:32 AM   #9
techguru666
LQ Newbie
 
Registered: Jul 2012
Posts: 24

Rep: Reputation: Disabled
The simplest alias should be:

Quote:
alias fnd='find . -name'
And then you can always execute:

Quote:
fnd test
 
Old 10-01-2012, 12:00 PM   #10
z1078516
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Smile fnd test

Thank you for everyones input.
I ended up just creating the alias statement:

alias fnd='find . -name'

I do not think I want to put a specific location
to look for files as this changes frequently.

Plus if I search from a high starting point, / (root level) for example,
this would take some time to respond back.
 
  


Reply



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
Help with "find" command in backup/zip script nixanwin Linux - General 4 02-21-2011 11:44 PM
[SOLVED] Errors executing shell script: "command not found" and "no such file or directory" eko000 Linux - Newbie 1 01-14-2011 07:54 AM
bash script: how to check stderr after command "find" Vilmerok Programming 1 07-08-2009 07:35 AM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 02:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:02 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration