Commandline quick tips: How to locate a file

We all have files on our computers — documents, photos, source code, you name it. So many of them. Definitely more than I can remember. And if not challenging, it might be time consuming to find the right one you’re looking for. In this post, we’ll have a look at how to make sense of your files on the command line, and especially how to quickly find the ones you’re looking for.

Good news is there are few quite useful utilities in the Linux commandline designed specifically to look for files on your computer. We’ll have a look at three of those: ls, tree, and find.

ls

If you know where your files are, and you just need to list them or see information about them, ls is here for you.

Just running ls lists all visible files and directories in the current directory:

$ ls
Documents  Music  Pictures  Videos  notes.txt

Adding the -l option shows basic information about the files. And together with the -h option you’ll see file sizes in a human-readable format:

$ ls -lh
total 60K
drwxr-xr-x 2 adam adam 4.0K Nov  2 13:07 Documents
drwxr-xr-x 2 adam adam 4.0K Nov  2 13:07 Music
drwxr-xr-x 2 adam adam 4.0K Nov  2 13:13 Pictures
drwxr-xr-x 2 adam adam 4.0K Nov  2 13:07 Videos
-rw-r--r-- 1 adam adam  43K Nov  2 13:12 notes.txt

Is can also search a specific place:

$ ls Pictures/
trees.png  wallpaper.png

Or a specific file — even with just a part of the name:

$ ls *.txt
notes.txt

Something missing? Looking for a hidden file? No problem, use the -a option:

$ ls -a
.   .bash_logout   .bashrc  Documents  Pictures  notes.txt
..  .bash_profile  .vimrc   Music      Videos

There are many other useful options for ls, and you can combine them together to achieve what you need.  Learn about them by running:

$ man ls

tree

If you want to see, well, a tree structure of your files, tree is a good choice. It’s probably not installed by default which you can do yourself using the package manager DNF:

$ sudo dnf install tree

Running tree without any options or parameters shows the whole tree starting at the current directory. Just a warning, this output might be huge, because it will include all files and directories:

$ tree
.
|-- Documents
|   |-- notes.txt
|   |-- secret
|   |   `-- christmas-presents.txt
|   `-- work
|       |-- project-abc
|       |   |-- README.md
|       |   |-- do-things.sh
|       |   `-- project-notes.txt
|       `-- status-reports.txt
|-- Music
|-- Pictures
|   |-- trees.png
|   `-- wallpaper.png
|-- Videos
`-- notes.txt

If that’s too much, I can limit the number of levels it goes using the -L option followed by a number specifying the number of levels I want to see:

$ tree -L 2
.
|-- Documents
|   |-- notes.txt
|   |-- secret
|   `-- work
|-- Music
|-- Pictures
|   |-- trees.png
|   `-- wallpaper.png
|-- Videos
`-- notes.txt

You can also display a tree of a specific path:

$ tree Documents/work/
Documents/work/
|-- project-abc
|   |-- README.md
|   |-- do-things.sh
|   `-- project-notes.txt
`-- status-reports.txt

To browse and search a huge tree, you can use it together with less:

$ tree | less

Again, there are other options you can use with three, and you can combine them together for even more power. The manual page has them all:

$ man tree

find

And what about files that live somewhere in the unknown? Let’s find them!

In case you don’t have find on your system, you can install it using DNF:

$ sudo dnf install findutils

Running find without any options or parameters recursively lists all files and directories in the current directory.

$ find
.
./Documents
./Documents/secret
./Documents/secret/christmas-presents.txt
./Documents/notes.txt
./Documents/work
./Documents/work/status-reports.txt
./Documents/work/project-abc
./Documents/work/project-abc/README.md
./Documents/work/project-abc/do-things.sh
./Documents/work/project-abc/project-notes.txt
./.bash_logout
./.bashrc
./Videos
./.bash_profile
./.vimrc
./Pictures
./Pictures/trees.png
./Pictures/wallpaper.png
./notes.txt
./Music

But the true power of find is that you can search by name:

$ find -name do-things.sh
./Documents/work/project-abc/do-things.sh

Or just a part of a name — like the file extension. Let’s find all .txt files:

$ find -name "*.txt"
./Documents/secret/christmas-presents.txt
./Documents/notes.txt
./Documents/work/status-reports.txt
./Documents/work/project-abc/project-notes.txt
./notes.txt

You can also look for files by size. That might be especially useful if you’re running out of space. Let’s list all files larger than 1 MB:

$ find -size +1M 
./Pictures/trees.png
./Pictures/wallpaper.png

Searching a specific directory is also possible. Let’s say I want to find a file in my Documents directory, and I know it has the word “project” in its name:

$ find Documents -name "*project*"
Documents/work/project-abc
Documents/work/project-abc/project-notes.txt

Ah! That also showed the directory. One thing I can do is to limit the search query to files only:

$ find Documents -name "*project*" -type f
Documents/work/project-abc/project-notes.txt

And again, find have many more options you can use, the man page might definitely help you:

$ man find

 

Using Software

19 Comments

  1. If you want to find files by content:

    grep -rH <query> <path>
  2. Lukas

    You forgot the most important tool in my opinion: locate. I quote from the Arch wiki:
    locate is a common Unix tool for quickly finding files by name. It offers speed improvements over the find tool by searching a pre-constructed database file, rather than the filesystem directly. The downside of this approach is that changes made since the construction of the database file cannot be detected by locate. This problem can be minimised by scheduled database updates.

  3. Robert Brady

    $ locate -b ‘\cat’

  4. If you find yourself often in the position to find specific files by their name, index based tools might give you a significant edge over the usage of ‘find -name’. Take a look at locate/slocate.

  5. Fred Weigel

    How about the venerable locate? (updatedb/locate)
    sudo dnf install locate

    queries like

    locate –all yellow ball

    which returns all filenames that contain yellow and ball.

    • RM

      Thanks for your comment. I knew about “locate” but did not know about the “all” switch!

  6. Mark Orchard

    Very useful, thanks for the information. I keep forgetting the “Tree” function.
    Is there an option with find to automatically cd to a file or directory once located?

    • Fred Weigel

      Look at the -exec action in the man page

    • Spike

      I don’t think so. How could it know which to cd if there are multiple matches?!
      You could write a function that uses the first result and cd into it, if it’s a directory,

    • Elliott S

      You might want to try installing

      fzf

      . In Fedora by default, only the tab completion is enabled, so you would type cd ** then press Tab, and it will open a list of directories from which you can start typing to pick one by pressing Enter.

      If you follow the instructions in the

      README.Fedora

      in the package, you can enable keyboard shortcuts as well. Then you can just type cd and press Ctrl+T to open the completion list directly.

  7. Nathan D

    Find all text files in all subdirectories where the phrase foo is used:

    find . -type f -name "*.txt" -exec grep -rn "foo" {} +

    [Editor’s note: fixed an unclosed double-quote-pair]

  8. Ray McCaffity

    There is also “which”
    which bash
    /usr/bin/bash

  9. Bartosz

    I recommend “silver searcher” (ag) for searching content of text files (code mainly), “fasd” for fast finding already used files/folders and completing other commands with it and fzf for great fuzzy searching.

    • Julian

      When mentioning fzf one could also mention rip grep (rg) as an very fast grep alternative and fd instead of find. With these tools one then can also navigate to files and directories.

  10. JdeH

    For file content, I find searchodt.sh and searchods.sh very useful for Open/LibreOffice document and spreadsheet files, respectively.

    For pdf files, pdfgrep is really good.

  11. Szymon

    Possible to find files by photo exif ? i.e. camera model ?

  12. EraZor

    Definately +1 for Julian’s tip of ripgrep. Ultra high performant, multithreaded search tool for grepping or finding files. Totally upped my searching experiance!

    If you have not: Take a look at it right now!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

The opinions expressed on this website are those of each author, not of the author's employer or of Red Hat. Fedora Magazine aspires to publish all content under a Creative Commons license but may not be able to do so in all cases. You are responsible for ensuring that you have the necessary permission to reuse any work on this site. The Fedora logo is a trademark of Red Hat, Inc. Terms and Conditions

%d bloggers like this: