Results 1 to 7 of 7
Hello everyone!
I'm new to bash scripting and I've searched around the forums and Internet for this but haven't had any luck. I've found similar things but not what I ...
- 06-14-2010 #1Just Joined!
- Join Date
- Jun 2010
- Posts
- 3
Bash script to locate a specific file
Hello everyone!
I'm new to bash scripting and I've searched around the forums and Internet for this but haven't had any luck. I've found similar things but not what I need. What I need to do is write a simple script that uses what the user inputs to locate and display where a file is. I would prefer to use locate instead of find since I know that the person I am writing this for has locate on her machine (my mom, who is just beginning with Linux).
I'm writing the script to make things easier for her while she learns. In this particular part of the script I would like to be able to have the script prompt her to enter the file she is searching for, read the her input and then display for her where the file is. I realize it would in most cases be much simpler just to teach her how to use locate, but she is very impatient and this is just a part of the script I will be writing, but I can't figure out how to do this. Any help would be greatly appreciated.
Thanks in advance!
- 06-14-2010 #2
something like this?
Code:echo -n "Name of file to locate: " read -e FILENAME locate $FILENAME
linux user # 503963
- 06-14-2010 #3Linux Newbie
- Join Date
- Mar 2010
- Posts
- 121
You can read input with the read command - here's a short example:
After that, using locate should be fairly simple. Although you may need to run updatedb to locate newer files, you can always allow the script do this by putting updatedb in /etc/sudoers as requiring no password, and you can run it if you can't find the file initially.Code:#!/bin/bash echo -n "Enter something: " read INPUT echo "You entered: \"$INPUT\""
- 06-14-2010 #4Just Joined!
- Join Date
- Jun 2010
- Posts
- 3
Thank you!
Thank you very much scathefire and JohnGraham! This is exactly what I needed! Thanks again, I do appreciated it!
- 06-15-2010 #5Linux Newbie
- Join Date
- Oct 2008
- Posts
- 140
My hat's off to your mom for learning to use a CLI. Wouldn't it be less typing for her to type locate filename than to type the name of a script and then it's parameter?
Maybe I just can't visualize the larger script you have in mind.
- 06-15-2010 #6Just Joined!
- Join Date
- Jun 2010
- Posts
- 3
You are right that it would be much less typing to just use locate, but she is new to Linux and is getting in the habit of calling me for help a little too often. Not that I mind helping her, I don't want to sound like a jerk. So, I'm writing this script as a sort of "help desk" sort of thing and locate is just part of it.
She won't have to type in any parameters for the program or even open the terminal. When I put it on her machine I'll create a launcher on her Desktop that she can just click on and it will open the terminal and start the program right away. My thinking is that if she sees some of the workings of the terminal, she won't be "afraid" of it and I can then more easily teach her things like locate, ls, df, who, whois and so on.
- 06-15-2010 #7


Reply With Quote
