Dr. Torq: Finding Things Under Linux
We humans always need to find things while computing. Maybe we want to know what machines are currently up and running on our local network. Linux can tell us that. Perhaps we need to sift through a bunch of files to find a certain keyword or tidbit of information. Linux can help in that situation, too. For that matter, we could simply be searching for a certain file, from a year ago on our Linux box. Punch in a handful of characters on the Linux command line and you’re in business.
Today, we’ll look at a collection of Linux commands to find things. These commands are universal across Linux systems, from the largest supercomputers all the way down to the lowly Raspberry Pi. We’ll start with a program that may or may not be in the standard load of applications on your version of Linux. It’s easy to fix by installing it with an application manager like synaptic or apt-get. All the rest are likely already installed in the core Linux build on your machine.
Find Your Network Address with nmap
Nmap is used to find IP addresses on your local network. You might use the IP to ascertain which machines are currently up and running on your network. The first thing to do is use another program, called ifconfig, to find the IP of your own machine.
Here we see that my machine has an IP of 192.168.1.109 on the enp6s0 (wired) Ethernet interface. Now just plug most of the address into the nmap command using a wildcard character.
rob% sudo nmap -sn 192.168.1.*
Be sure to use the sudo root command, otherwise you’ll likely not get all the addresses. Additionally, sudo allows nmap to query the machine manufacturer information. As you can see, the 192.168.1.40 device is a Hewlett-Packard. I’ve set up a laser printer with that address, for network printing.
The “-sn” option helps simplify the output a bit.
You’ll also notice that some devices show an unknown manufacturer. You might have to dig into the data a little more using Wireshark or go searching for rogue devices if you can’t relate the IP to one of your known machines.
Let’s next look at grep.
Search for a String with grep
Grep is a powerful tool for finding text strings in a file. The basic command is simply grep followed by the text string, then the file name you want to search through. Here’s an example.
rob% grep LinuxToday robnet.htm
If you want to know the particular line of text in the file, add the “-n” option to get the line number of each instance found. Also, remember that Linux recognizes upper and lower case letters as separate and distinct characters.
I save web page addresses and their titles in a series of files, under my “ideas” directory with dates in the file name. Examples might include 12052019.txt or 07192018.txt. The web address and title are easy to copy and paste into a text file using CTRL-C (copy) and CTRL-V (paste), respectively. It also works with the copy/paste capabilities in the Firefox and Chrome browsers.
Then if I want to find a certain keyword, using either the title or web page address, I just need to use the grep command with a wildcard character, to search through all the files in one shot. Check this example.
rob% grep innovation *.txt
Grep lets you search for strings in text, html and even binary files.
Moving on to searching for files, let’s look at find and locate.
Discover Your File with find
The find command methodically steps through your directories to find the file you seek. Say we want to find the 11222017.txt file. Let’s say we are in my home directory, which is /home/rob.
We could issue the following.
rob% find . -name 11222017.txt
The “.” tells find to start in the current directory and search downward through the file system.
There it is at the bottom, along with the pathname, from the current directory.
If you want to search through the whole partition use the “/” instead of the “.” for the starting directory. There’s no real easy way to suppress the “permission denied” messages. I just ignore them.
The thing to remember about find is that it steps through the directories, each time you search. This can take some time if you have a lot of directories, sub-directories and long file names. Starting from the root (/) directory for the above text file took about a minute in a 100+ GB partition on my antique ASUS duo-core notebook. Faster file searches are possible with the locate command, although there is a trade-off.
Find Your File Quickly with locate
Locate queries a cache that it uses to map out files, instead of stepping through the file tree structure, during a search. It updates the database once a day or so using the updatedb command.
Suppose we are back at my home directory and want to find that text file from 2017. Here’s the command line.
rob% locate 11222017.txt
Locate only took about two seconds to return the location of the file. Notice that it also displayed the full path.
While locate is fast, keep in mind that if you have files that were created or copied over to your file system within the last 24 hours, they may not show up in the results.
We’ve looked at a few commands and techniques that I regularly use to find things under Linux. You can always use the “–help” option, on all the commands to get more information on usage. The “man” pages are helpful too, when you are learning to use programs on the Linux command line. It’s a little awkward and quirky, at first. Keep at it and practice. You might even keep a little notebook or cheat sheet of frequently used commands and options until they become second nature.
Feature image: “Rama and Lakshmana Search in Vain for Sita: Illustrated folio from a dispersed Ramayana series,” ca. 1680–90, India, Rajasthan, Mewar, New York Metropolitan Museum of Art.