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

Fedora Makes Progress On Their New Modularity Concept

After abandoning their Fedora Server 27 Modular Edition work last year, Fedora developers interested in modularizing Fedora packaging have drawn up new plans that are now approved by the Fedora Council. At Wednesday's Fedora Council meeting, the new Fedora modularization plan was approved. The goal outlined by "Objective: Fedora Modularization — The Release" is "Modularity will transform the all-in-one Fedora OS into an operating system plus a module repository, which will contain a wide selection of software easily maintained by packagers. This iteration of the Objective focuses on the second part — providing a wide selection software in various versions — while laying the groundwork for the first." The new Fedora Modular plans no longer involve modularizing the entire distribution but rather "traditionally built packages" will remain and only components benefiting from modular features would be modularized. The components targeted are things like database servers, web servers, Node.js, etc, where users may prefer sticking to one particular version of a program and not upgrade until it's end-of-life or has other particular reasons to want to move on to a newer version. Read more Also: PHP version 7.1.14RC1 and 7.2.2RC1

Games: Slay the Spire, OVIVO, Unity

  • Slay the Spire fuses together a roguelike with a card game, it's rather fun
    What do you get when you throw cards at a roguelike? Slay the Spire [Steam, Official Site] answers that question and then some! Do note, that it's currently in Early Access so it's still getting new content updates and bug fixes. Here's some initial thoughts after spending some time with it.
  • OVIVO is a platformer with some rather unusual mechanics, it's also now on Linux
    I've played many platformers in my time and OVIVO [Steam, Official Site] stands quite tall as something rather unique in a sea of games.
  • Unity Game Engine Working On Graphics Rendering Improvements For 2018
    The Unity game engine has a New Year's resolution of improving its graphics renderer abilities in 2018.
  • [Unity] 2018 and Graphics
    The Unity 2018 release cycle will have a heavy focus on graphics! There are a number of features that will be released during 2018 that will drastically enhance Unity’s rendering capabilities. Hopefully you will find a new feature for every type of user, such as the visual tools for artists or more powerful rendering control for low-level engineers. We are very excited to share with you what we are working on, and we can’t wait to see what you’ll make with it!

Wine 3.0

  • Wine 3.0 Released
    The Wine team is proud to announce that the stable release Wine 3.0 is now available.
  • Wine 3.0 Officially Released with Android Driver, Direct3D 11 and 10 Support
    The Wine (Wine Is Not an Emulator) project has been updated today to version 3.0, a major release that ends 2017 in style for the open-source compatibility layer capable of running Windows apps and games on Linux-based and UNIX-like operating systems. Almost a year in the works, Wine 3.0 comes with amazing new features like an Android driver that lets users run Windows apps and games on Android-powered machines, Direct3D 11 support enabled by default for AMD Radeon and Intel GPUs, AES encryption support on macOS, Progman DDE support, and a task scheduler.
  • Wine 3.0 Released With Initial Direct3D 11 Support, D3D Command Stream
    The Wine camp has officially released Wine 3.0 as their annual feature update to this program for running Windows games/applications on Linux and other operating systems.
  • The big Wine 3.0 release is now officially available
    Good things come to those who wait, like a fine Wine. Today the Wine team has officially release the next stable version Wine 3.0 [Official Site]. After around a year of development during the 2.x cycle, Wine 3.0 brings in some major changes towards better game and application support for those of you wanting to run Windows-only stuff on Linux. It's nowhere near perfect, but it's a massive advancement for the Wine project and provides a good base for them to continue onwards.

today's leftovers

  • Amazing Facts about Linux Operating System You Probably Don't Know [Ed: This gets some facts wrong, right from the very first sentence]
    It was almost 20 years ago when the first version of Linux came into the market and since then, this operating system has made its important stature beside Microsoft Windows. Linux has turned out to be one of the most acknowledged and extensively used operating system. Enthused by UNIX, Linux has smartly managed to attract a lot of tech giants such as Facebook, Google, Yahoo, Twitter, Amazon, and much more. However, when it comes to assessing the exact rate of adoption of Linux in the market, the task is a bit tough since the sources to get copies are wide in number. Appreciating workers' and developers' hard-work, Linux has been designed in such a way that exploring and learning things on this operating system has become quite captivating and enthralling. In this post, let's know more about amazing features and facts of this operating system.
  • MenuLibre 2.1.4 Released For Menu Editing On GNOME/LXDE/Xfce/Unity
    MenuLibre is an advanced menu editor that supports not just one desktop environment but GNOME, LXDE, Xfce, Cinnamon, and Unity Linux systems. Today's MenuLibre 2.1.4 for advanced menu editing of Linux desktop systems has a new "test launcher" option, new sorting abilities for menus, new layout preferences for desktops supporting client-side decorations, improved file handling, and many bug fixes.
  • EU Makes EUR 1B Bid to Boost Supercomputer Efforts
    The market for High-Performance Computing (HPC) has increasingly been dominated in recent years by China. Now the European Union (EU) is aiming to get back into the hunt with a new initiative called the EuroHPC Joint Undertaking. The goal of the EuroHPC effort is to acquire, build and deploy a world-class High-Performance Computing (HPC) infrastructure. The effort will also involve the development of application software that will run on the HPC infrastructure. The EU will contribute EUR 486 million, which will be matched by Member States and associated countries. According to the EU, approximately EUR 1 billion in total will be invested in the effort by 2020.
  • EasyLinux Show 18.2 | Meltdown, Spectre and Linux Mint
  • Videos on Samba shares
    A longstanding complaint about KDE Plasma is that it’s a pain in the butt to stream videos that are located on Samba shares. It’s a usability issue for sure. I’d like to talk a bit about the origins of the problem and how I helped drive a solution.
  • 3 Growth Stocks to Buy and Hold for 25 Years
  • Swing Trading Earnings Bullish Momentum With Options in Red Hat Inc
  • 10 Best Android Cleaner Apps For 2018