Language Selection

English French German Italian Portuguese Spanish

Tutorial: Using the 'find' Command

Filed under
HowTos

GNU find is a powerful command-line utility that lets you search for files and folders in a hierarchical tree directory structure. It is the backend for all those utilities out there like the graphical searching in KDE or GNOME. However, find can be a little hard to handle at first by beginners. In this tutorial I will try to explain some of the capabilities of find, show some useful one-liners and provide more explanations regarding this command.

In this tutorial I will start from the basic ways of using find and head up into showing more complicated (but very useful) ways of getting the most out of it, in order to search and display exactly the results that you are looking for. The version of find that I currently have installed is 4.4.2, as it comes with Ubuntu 12.04 Precise Pangolin, and Bash 4.2.20 (older versions should work without problem too). Special thanks go to http://www.commandlinefu.com/ for some really great one-liners.

If you’re not familiar with the terminal, command-line or Linux in general I suggest you read my introductory tutorial here: Introduction to Linux Command-Line for Beginners.
The Basics

The simplest way of using find is by typying it in a terminal:

find

This will list all the files and folders (including hidden ones and their sub-files and sub-folders) in the current directory, following the whole hierarchical structure. This will usually generate a long list of files and doesn’t seem to give us much. It’s exactly the same as:

find .

Where . is the currently working directory. This will list all the files and folders in the currently working directory.

It’s probably best to use a new folder somewhere in the file system to see this in effect, a folder which doesn’t have many sub-folders and files.

Moving on, let’s search for all the files that include the name profile in their filename:

find . -name *profile*

* is a wildcard that replaces any number of characters or no character. The above command searches in the current folder for the name *profile*.

find /usr/share -name FreeSans*

This will search inside /usr/share for all the files that start with FreeSans (and end in whatever characters e.g. FreeSans.ttf). I recommend using double quotes around the pattern to search for e.g. find . -name “.bash*”.

Another example:

find /usr/share -name FreeSans* | grep Oblique

So now you know how to search for a certain filename in a specific location.
Uppercase/Lowercase

Sometimes you need to ignore uppercase and lowercase and just search for text by ignoring case-sensitive. We’ll to this just by replacing -name with -iname:

find /usr/share -iname FREESANS*

Date

find . -mtime +3 -iname *somefile*

This will search for files that were created earlier than 3 days ago.
Get only the filename instead of whole path to the file

find will return the whole path to the files that match the search pattern, so in order to get only the filename you can use the fprintf argument command:

find /usr/bin -name "alsa*"

To get only the filename, use:

find /usr/bin -name "alsa*" -printf "%f\n"

By size

To search for files by size, use the -size argument, for example:

find /usr -size +500k -name "*png"

This will search inside /usr for files which are equal to or larger than 500 KB and are ending in png. Another example:

find /usr -size +1M -name "*png"

Which will search for files which are bigger than 1 MB in size. Instead of the plus sign, you could use minus in order to search for files that are smaller than a specified size:

find /usr -size -10c -name "*png"

The -10c specifier tells find to only display files which are smaller than 10 bytes. Don’t forget the + or – preceding the desired filesize.
Automatically list details about the found files

You could use a pipe and the xargs command for this:

find /usr/lib -size +2M -name "*.so" | xargs ls -lh

Notice that this will list the files in the current directory if find returns no file.
Searching for files than contain specific text

This is probably one of the most useful ways to search for some file which name you’ve forgot but you know some of the text it contains inside.

find . -name "*bash*" -exec grep -l "aliases" {} +

This will search in all the files that contain the patter bash for the word aliases. Those files that contain this pattern will be printed out.
Some useful one-liners

Find top 20 largest files:

find . -type f -print0 | xargs -0 du -h | sort -hr | head -20

References

Special thanks go to http://www.commandlinefu.com/ and the following articles:

https://www.linux.com/learn/tutorials/316404-10-tips-for-using-gnu-find
http://www.ibm.com/developerworks/aix/library/au-unix-find.html
http://www.linux.ie/newusers/beginners-linux-guide/find.php
http://www.cyberciti.biz/faq/howto-recursively-search-all-files-for-words/

http://www.tuxarena.com/2012/03/tutorial-using-the-find-command

More in Tux Machines

Leftovers: Software

  • [Video] Linux Audio Programs Compared 2017
    I made this video for those that are new to, or just interested in making music on the Linux OS. I go over the features, goods and bads of Rosegarden, LMMS, Ardour, Mixbus, and EnergyXT, as well as touch on Qtractor. I don't don't go much into details of the particular versions I am using, but the video was made in the early part of 2017 and I'm running Ubuntu 16.04LTS.
  • Green Recorder: A Simple Desktop/Screen Recorder for Linux
    Green Recorder is a simple, open source desktop recorder developed for Linux systems built using Python, GTK and FFmpeg. It supports most of the Linux desktop environments such as Unity, Gnome, Cinnamon, Mate, Xfce and so on. Recently it has been updated to work with Wayland too in Gnome session.
  • Komorebi: A New Way To Enhance Your Desktop Using Animated/Parallax Wallpapers
    In past there were applications that allowed us to run videos/Gif as wallpaper on the desktop and make desktop look much cooler but than all of sudden the development of such Apps stopped and I can't name any App that exist for this purpose. Komorebi is fairly new application designed to make your desktop experience much better and make desktop cool as well, we can say it is kind of 'live wallpaper' situation here or 3D wallpaper. It is developed by Abe Masri and available under GPL license for free.
  • Stacer Sytem Optimizer: A Must Have Application For Ubuntu/Linux Mint
    There are multiple ways to optimize your Linux, the most geeky way is using Terminal, there are also applications available that performs such actions like Bleachbit, Ubuntu cleaner and so on. Stacer is simple, open-source, quick and new application designed to offer you all-in-one optimizer for your Ubuntu/Linux Mint (It's alternative to CCleaner but only for Linux).
  • Qtox: Open Source and Fully Secure Skype Replacement for Linux
    Long years ago, we've talked about a Skype alternative called Tox which was still in its early developmental stages. Tox was supposed to become the anti-thesis of Skype by being a fully open-source video and voice chat client that placed user privacy and security at its center. Well, guess what, there are now fully active and well-maintained chat clients that are built on top of Tox protocol. qTox is one of them.
  • Rclone 1.36 Released With SFTP And Local Symlinks Support, More
    Rclone 1.36 was released recently, bringing support for SFTP, local symbolic links support, mount improvements, along with many other new features and bug fixes. For those not familiar with Rclone, this is a cross-platform command line tool for synchronizing files and folders to multiple cloud storages, which supports Dropbox, Google Drive, Amazon S3, Amazon Drive, Microsoft One Drive, Yandex Disk, and more. It can be used to sync files either from your machine or from one cloud storage to another.
  • Streamlink Twitch GUI 1.2.0 Adds Support For Communities And Team Pages, Basic Hotkeys
    Streamlink Twitch GUI (previously Livestreamer Twitch GUI) is a multi-platform Twitch.tv browser. The application is powered by Node.js, Chromium and Streamlink, though it can still use Livestreamer (which is no longer maintained) too.
  • Code Editor `Brackets` 1.9 Released, Available In PPA
    Brackets is a free, open source code editor focused on front-end web development (HTML, CSS and JavaScript).
  • Terminix Terminal Emulator Renamed To Tilix, Sees New Bugfix Release
    [Quick update] Terminix, a GTK3 tiling terminal emulator, has been renamed to Tilix due to some trademark issues.

today's howtos

Games and CodeWeavers/Wine

  • A Snapshot of Linux Gamers, Just One Year Ago
    It’s about time we share the analysis of that Q1 2016 survey (fielding occured in March last year), especially as we are about to launch the Q1 2017 one pretty, pretty soon. That way we will be able to compare how things have changed over the course of 12 months. As usual, the whole disclaimer about online surveys is valid here (data is only as good as your n size, the appropriateness of your sampling, and the quality of the responses, etc…), but assuming it’s not all that bad and all that unreliable, let’s dig in the results. As a reminder, most of the respondents for this survey were recruited through the r/linux and r/linux_gaming subreddits, as well as the readership of BoilingSteam. This is not our first survey, and you can see our previous ones done in the second quarter of 2015, and the following one in the last quarter of 2015.
  • Slime-san Coming To PC, Mac and Linux
    Headup Games and Fabraz proudly announce their upcoming action-platformer Slime-san for PC, Mac and Linux via Steam & Humble Bundle. Console releases will follow soon after. Jump and slime your way through 100 levels in a unique 5-colored, pixelated world and escape from a giant worm’s innards. Get your shopping done in Slumptown, a town full of survivors within the worm. Unlock different play styles, outfits, shaders and even multiplayer mini-games! Slime-san is developed by Fabraz, an independent development studio that also released the critically-acclaimed games Cannon Crasha and Planet Diver. Slime-san was minding his own business, sliming around in a peaceful forest when suddenly…A giant worm appeared and gobbled him up! Now deep within the worm’s belly, Slime-san has to face a decision: Be digested by the incoming wall of stomach acid... Or jump, slide and slime his way through the worm's intestines and back out its mouth!
  • CodeWeavers Announces CrossOver 16.2.0
  • The Wine Revolution is ON!
    As you know Codeweavers (and other WINE contributors) have been working on DX11 support for a while – they were supposed to have DX11 support by the end of 2016, but as with all complex projects, timelines tend to slip and only very DX11 titles could run a few months ago. Since then, there was no major announcement, but it seems that the progress has been very significant in the recent WINE versions (2.3 is already out).

Leftovers: KDE