Uni Lab confusing me. Please Help

John Harrison

New Member
Joined
Sep 24, 2017
Messages
3
Reaction score
0
Credits
0
Hi, so I'm doing a uni course on networking and one of the modules is Linux. Some of the information in the labs makes it quite difficult to find online examples so I came here in hope of finding some good nature'd people to help me.

I have been using the find command to find where information is in certain files.

# find ./ -type f -exec grep -l 'fsck' {} \; is one example and I'm fine with that and how it returns...

./mke2fs.conf telling me that's where it found 'fsck'...

The thing I'm having trouble with is using this to locate and run the gnome-calculator using only the find command. I'm running debian 9.1.0 and I'm not having much luck figuring this out.

I've tried a bunch of different stuff that hasn't worked and the man pages don't seem to contain this information. I know about the -execute parameter but I don't know where to put it to get it to work.

I've navigated to the /usr/bin directory where the applications is stored and run this command.

# find ./ -type f -exec grep -l gnome-calculator {} \;

But it's not working. Any help would be greatly appreciated and thanks in advance.
 


If I use
Code:
sudo find ./ -type f -exec 'gnome-calculator' \;
it will ONLY open gnome-calculator - not show where the executable file is.

You can achieve the same result, JUST open gnome-calculator, by just inputting
Code:
gnome-calculator

Whereas
Code:
sudo find / -name 'gnome-calculator' -print -exec {} \;
will tell you WHERE the gnome-calculator file is as well as open it.

Other related commands are
Code:
whereis
which will tell you everywhere a file occurs e.g.
Code:
whereis gnome-calculator
and which will tell you where the executable file occurs e.g.
Code:
which gnome -calculator
 
I ended up just playing around for a while and eventually I got it with this...

Thanks for the reply though that link would have also solved it. Cheers

find ./ -type f -exec 'gnome-calculator' \;

To anyone Studying at UWS, You're welcome JSH* lol

That particular find example searches for ANY files in the current directory (and all sub-directories) and then runs gnome-calculator for each file that it finds (and because the exact path to gnome-calculator is not specified in the -exec section of the find command, the system will search through all directories listed in $PATH and execute the first instance of gnome-calculator that it finds. If you don't have gnome-calculator installed, bash will issue a "command not found" error each time a file is found!) So that particular usage of find would definitely NOT solve your problem. You'd end up with an instance of gnome calculator running for every file found in the current directory (and all subdirectories), or a bunch of "command not found" error messages.

Arochesters example:
Code:
find / -type f -name 'gnome-calculator' -print -exec {} \;

Actually searches from the root of the file-system to find all files called gnome-calculator. Each time it finds a file called gnome-calculator (hopefully only once!), it will print the path to the file and then attempt to execute it. Which is pretty much exactly what you are looking for!

But - if you have an ordinary text file in your home directory called gnome-calculator, the above find command would execute the actual gnome-calculator executable (usr/bin/gnome-calculator) and then would try to execute the text file ~/gnome-calculator... Which would just leave you with a "Permission denied" error message. To prevent this from happening, you could also add the -executable flag too. Then find would only match executable files that were called 'gnome-calculator':
Code:
find  / -type f -name 'gnome-calculator' -print -executable -exec {} \;

Also, if you aren't searching as root - when find gets into certain directories (e.g. /sys, /srv, /proc, /tmp) you will get "permission denied" error messages. To avoid seeing these - you can add extra parameters to find to exclude them from the search:
Code:
find / \( -path /sys -o -path /srv -o -path /proc -o -path /tmp \) -prune -o -type f -name 'gnome-calculator' -executable -print -exec {} \;


Finally - If looking for an executable that should be installed somewhere in $PATH, then "which" or "whereis" would be the best commands to use - as recommended by Arochester.

There is also the locate command. Locate can do a more 'fuzzy' search and will show all files that match (or even partially match) a particular pattern. But locate relies on databases produced by the updatedb command. if updatedb hasn't been run for a while - files created since the last time updatedb was ran will not be listed anywhere in the databases and will not be found. So before using locate, it can be an idea to run "sudo updatedb" to update the databases.

Or you could just use find! :)
 
Last edited:
Thank you very much kind sir. That has saved me hassle. I have a task which makes me use find to find and then execute a program but I wasn't properly understanding it until now. Cheers :)
 

Members online


Top