Linux command line quick tip for locating a file

  • 2020-12-19 21:23:29
  • OfStack

We all have files stored on our computers -- directories, photos, source code, and so on. There are so many of them. Beyond my memory, no doubt. If you have no goals, finding the right one can be time-consuming. In this article, let's look at how to find the required file on the command line, especially quickly finding the one you want.

The good news is that the Linux command line has many very useful command-line tools designed to find files on your computer. Let's take a look at three of them: ls, tree and find.

ls

If you know where the files are, you just need to list them or view information about them, and ls was created for that purpose.

Just run ls to list all visible files and directories in the current directory:


$ ls
Documents Music Pictures Videos notes.txt

Add the -ES20en option to view information about the file. With the -ES21en option, you can see the file size in a format that is easy for people to read:


$ 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

ls can also search for 1 specified location:


$ ls Pictures/
trees.png wallpaper.png

Or 1 specified file -- even if only followed by 1 part of the name:


$ ls *.txt
notes.txt

What's missing? Want to view a hidden file? No problem, use the -ES35en option:


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

ls has many other useful options that you can combine to get the results you want at 1. You can learn more by using the following command:


$ man ls

tree

If you want to see the tree structure of your files, tree is a good choice. It may not be installed by default on your system, but you can manually install DNF using package management:


$ sudo dnf install tree

If you run tree without any options or parameters, it will start with the current directory, showing a tree diagram containing all the directories and files under it. Reminder 1: This output can be very large because it contains all the directories and files in this directory:


$ 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 too many files are listed, use the -ES57en option followed by the number of levels you want to see to limit the number of levels listed:


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

You can also display a tree diagram of a specified directory:


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

If tree lists a large tree, you can combine it with less:

$ tree | less

Again, tree has a number of other options that you can use, and you can combine them to play a more powerful role in 1. The man man page has all of these options:

$ man tree

find

What if you don't know where the file is? Let's find them!

If YOU do not have find on your system, you can install it using DNF:


$ 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
0

When you run find without adding any options or parameters, it recursively lists all the files and directories in the current directory.


$ 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
1

But the real power of find is that you can search using file names:


$ 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
2

Or just 1 part of a name -- like a file suffix. Let's find 1 all the.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. This can be especially useful if you run out of space. Now list all files greater than 1 MB:


$ 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
4

You can also search for a specific directory. Let's say I want to find a file under my Documents folder that I know has the word "project" in its name:


$ 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
5

In addition to files it also shows directories. You can limit only search query files:


$ 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
6

Finally, find has many more options for you to use, and if you want to use them, the man man page will definitely help you:


$ 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
7

conclusion


Related articles: