Ottawa PC Users' Group (OPCUG)
 
   Home
   Reviews

 

   Copyright and Usage

   Privacy Policy

   Contact Us

 

Exploring Linux – Part 23

by Alan German

For me, one of the great mysteries in Linux has always been how to find specific files within the overall file system. I consider myself to be reasonably well organized in terms of file storage so I have never really had to delve into the various file search commands that Linux has to offer. But, recently, I came across a blog entry discussing how to use the “locate” command to search for files (http://linuxaria.com/howto/find-files-on-linux-with-the-command-locate). It turns out that locate needs an updated database of file names in order to identify current files. Now, while it is relatively easy to create and maintain such a database (e.g. with a script file and a chron job), my preference is for something a little simpler.

The basic problem is that most simple commands (and graphical file managers) only search inside the current file folder, so we need to be a little more creative. Enter the “find” command. This too, by default, searches in the current folder, but it is a simple task to extend the range of the search. In addition, find is such a powerful command that, once we specify a desired path, it will do all of the work for us, automatically scanning the entire directory tree structure below the specified path.

But, before we continue, let me set the stage on my particular requirements. The downside to the find command is that it can be slow, precisely because, by default, it does search through the entire tree structure. However, in my case, all of my data files are stored in a single partition, and the total disk space used in this partition rarely exceeds 2 GB. In consequence, the number of files and folders to be scanned is quite manageable. Combine this with a fast CPU, tons of memory, and a solid state disk, finding any given file across the entire data partition is pretty much instantaneous. So, a simple find command easily suffices for my purposes.

The data partition is mounted as /media/DataDisk in the root directory, whereas I am normally issuing the find command from my home folder. Consequently, I use the command in the form:

find ../../media/DataDisk filename

where the “../” pair changes the folder from home to root in order to access the data partition and then search for whatever “filename” is of current interest. The filename string can be specific to a given file, or can include wildcards to broaden the scope of the search.

Now, this command is still too complex for me to remember and, since I only need to use it occasionally, I need an even simpler method. My solution has been to create a bash script file to ask for the filename string as a user input and then automatically include this in a search across my data partition.

The complete script file is as follows:

#! /bin/bash
# Find command used from the home folder in all sub-folders of DataDisk
# for a case insensitive search of a specific file name
echo 'Enter file name'
read filenam
find ../../media/DataDisk -iname $filenam
echo "Shell command complete"
read

Note that the script requests the search string as the variable “filenam” and then includes this search string in the subsequent find command by calling up the variable as “$filenam”. Note also that the -iname switch has been used in order to make the search process case insensitive.

The script file is saved as filefind.sh and this file has its executable bit set so that the script will run in Terminal. The following screenshot shows the results of searching for any files with names that include “ticket”.

 

 

This is just a simple example of the use of the find command. Additional criteria can readily be added to make searches quite specific and/or wide ranging. Furthermore, it is even possible to combine these with subsequent file actions such that, for example, files bigger than 250 MB and more than six months old, can be deleted (http://www.debuntu.org/how-to-find-files-on-your-computer-with-find). As noted earlier, find is a very powerful command!


Originally published: November, 2012


top of page

 

 

Archived Reviews

A-J

K-Q

R-Z

 

The opinions expressed in these reviews
do not necessarily represent the views of the
Ottawa PC Users' Group or its members.