LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to create a script using the "find" command (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-create-a-script-using-the-find-command-4175429513/)

z1078516 09-28-2012 08:49 PM

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

sackboy 09-28-2012 09:14 PM

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


Rupadhya 09-28-2012 09:32 PM

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

z1078516 09-28-2012 09:40 PM

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.

Rupadhya 09-29-2012 12:30 AM

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

cbtshare 09-29-2012 12:41 AM

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

David the H. 09-30-2012 09:46 AM

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.

suicidaleggroll 09-30-2012 10:54 AM

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

techguru666 09-30-2012 11:32 AM

The simplest alias should be:

Quote:

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

Quote:

fnd test

z1078516 10-01-2012 12:00 PM

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.


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