Many of you fellow Ubuntu users will be familiar with the “Search for Files” tool that allows you to look for files. As is true with most things in Linux, there are great desktop tools, but more power can be found in Terminal than any streamlined desktop tool can match.
Today, I’d like to introduce you to a few tools that can turn a chore of finding files into an easy process.
The first tool that you should become comfortable with is also one of the simplest. The
locate program works in a similar fashion to most graphical search tools.
Let’s say that you’ve read up on how to modify X to provide enhanced video performance. The document that you are reading says to modify the
xorg.conf file, but the document doesn’t say where that is and you don’t know how to find it. You can use
locate to find it for you quickly:
[chris@home ~]$ locate xorg.conf /etc/X11/xorg.conf /usr/share/man/man5/xorg.conf.5.gz /var/lib/x11/xorg.conf.md5sum /var/lib/x11/xorg.conf.roster
It’s clear that
/etc/X11/xorg.conf is the file we’re looking for, but what if dozens or even hundreds of results were returned? How could you manage all of that? If you know a little bit about regular expressions (every Linux user really should learn the basics), you can easily use regular expressions to search for directories or files. For example, we can search for an exact match for
xorg.conf with the following command:
[chris@home ~]$ locate -r '/xorg.conf$' /etc/X11/xorg.conf
If you want to look for files without case sensitivity, you can use the
-i option. For example:
[chris@home ~]$ locate -i 'monthly report' /home/chris/Documents/August Monthly Report.doc /home/chris/Documents/July Monthly Report.doc /home/chris/Downloads/latest monthly report.doc
Something that may confuse you when you first start using
locate is that it not only returns matches, but if the match is a directory, it also lists all of the directories and files under that directory. For example:
[chris@home ~]$ locate Desktop /home/chris/Desktop /home/chris/Desktop/Head Shot.jpg /home/chris/Desktop/Skype.lnk /home/chris/Desktop/Steam.lnk ...
That command returns more than a thousand entries on my system. To reduce the results down to a list of entries that match the search term without all the contained files and directories, use the
[chris@home ~]$ locate -b Desktop /home/chris/Desktop /home/chris/bin/recordSoundAndDesktop.sh /opt/HipChat4/lib/QtQuick/Controls/Styles/Desktop /usr/include/qt4/QtGui/QDesktopServices /usr/include/qt4/QtGui/QDesktopWidget /usr/lib/libreoffice/program/wizards/common/Desktop.py /usr/lib/libreoffice/share/gallery/computers/Computer-Desktop.png /usr/lib/python2.7/dist-packages/xdg/DesktopEntry.py /usr/lib/python2.7/dist-packages/xdg/DesktopEntry.pyc /usr/lib/python3/dist-packages/xdg/DesktopEntry.py /usr/lib/python3/dist-packages/xdg/__pycache__/DesktopEntry.cpython-35.pyc /usr/lib/x86_64-linux-gnu/qt5/qml/QtQuick/Controls/Styles/Desktop /usr/share/kde4/apps/kio_desktop/DesktopLinks /usr/share/man/man3/File::DesktopEntry.3pm.gz /usr/share/perl5/File/DesktopEntry.pm /usr/share/sounds/KDE-Window-All-Desktops-Not.ogg /usr/share/sounds/KDE-Window-All-Desktops.ogg
There are still quite a few matches, but we no longer have a sort through all the extra results to find what we’re looking for.
locate command relies on a slocate database to function. This database caches the file system directory and file names and allows the
locate command to operate in moments rather than taking long periods of time to search the entire file system.
This ability doesn’t come without cost though. By default, your system will run a command to update this database once a day at 6:25am. This command will update the database with any changes made to the file system since the last update.
If you’ve made changes recently, such as added or removed large amounts of files and directories, and you want to use the
locate command to search through these new directories and files, you can manually update the slocate database at any time with the following command:
[chris@home ~]$ sudo updatedb
Depending on the volume of changes made since the slocate database was last updated, this could take a few seconds to a few minutes to update. Once it’s done, you’re ready to search again with
locate is great for searching the entire file system. However, sometimes you just want to find where a command lives. That’s where
which comes into play.
which only does one thing, but it does it well. Given the name of a command, which will tell you where the file for that command resides in the file system.
Let’s imagine that we want to find out where the
firefox command resides. We can simply run the following to find out:
[chris@home ~]$ which firefox /usr/bin/firefox
Why would you ever want to search specifically for a command? Imagine that you have some code that needs to replace an old command. You really need to remove the the old one (I usually rename it to the same name followed by “.bak” to keep it around in case I need it) first before putting the new one in its place.
which will help you find the file quickly and easily so you don’t have to search everywhere to find it.
whereis command is similar to which but goes one step further. Not only does it tell you where the executable file is located at, but it will also locate the source, man page, and other associated directories as well.
Keeping with the
which example, let’s ask
whereis about the
[chris@home ~]$ whereis firefox firefox: /usr/bin/firefox /usr/lib/firefox /etc/firefox /usr/share/man/man1/firefox.1.gz
This helped me find
/usr/lib/firefox/plugins earlier when working on the solution for Flash as that location is where the global add-ons for Firefox are stored.
find is the ultimate search tool, the Swiss Army knife of Linux search if you will.
find can do everything from search for files based on last modified times, owners and groups, permissions, type (file, directory, symbolic link, etc), whether the file is readable/writable/executable by the current user, file size, whether the file or directory is empty, and much more. You can also search for matches by regular expression.
When you use this tool in combination with the
xargs command (which I’ll have to cover some other time), you can do truly amazing things on the command line. Imagine being able to recursively search through the current directory for files ending with “.bak” that were last modified more than 100 days ago and then deleting them with one command:
[chris@home ~]$ find . -name '*.bak' -type f -mtime +100 -print0 | xargs -0 /bin/rm -f
It will look very complex at first, but when you master the art of using
find, it will become second nature. Soon, you’ll wonder how you ever searched for things without it.
To be frank, there is much more to find than I am willing to cover and there are many, many great introductions to the tool that do a better job than I would. So, I’ll simply point you to three great references that can be used to get going with find.
- Example uses of the Linux Command find
- (I apologize for the horrible colors in advance) Unix/Linux “find” Command Tutorial
- Using the find command