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 |
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 |
You could try something like this...
Code:
#! /bin/bash ## 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 |
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. |
Try this..
Code:
chmod +x ~/scripts/fnd Alternatively, you can put your scripts directory on the path with this.. export PATH=$PATH:~/scripts - Raj |
simply create an alias
script Quote:
then at the command line , just do Quote:
|
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(){ You could even expand it to include the starting directory. Code:
fnd(){ Code:
alias fnd='sh test.sh' 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 |
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' 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 |
The simplest alias should be:
Quote:
Quote:
|
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. |