Linux command line quick tips: How to locate a file

Linux command line quick tips: How to locate a file

We all have files stored on our computers -- directories, photos, source code, etc. There are so many of them. It is undoubtedly beyond my memory. Without a goal in mind, finding the right one can be time-consuming. In this article we'll look at how to find the files you need in the command line, specifically how to quickly find the one you want.

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

ls

If you know where the files are and you just want to list them or view information about them, ls is made for that.

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

$ ls
Documents Music Pictures Videos notes.txt

Add the -l option to view information about the file. Also add the -h option to view the file size 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

ls can also search a specific location:

$ ls Pictures/
trees.png wallpaper.png

or a specific file - even if only part of the name is followed by:

$ ls *.txt
notes.txt

What's missing? Want to view a hidden file? No problem, use the -a 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 effect you want. You can use the following command to learn more:

$ man ls

tree

If you want to view the tree structure of your files, tree is a good choice. Maybe it is not installed by default on your system, you can install it manually using the package manager DNF:

$ sudo dnf install tree

If you run tree without any options or arguments, it will start with the current directory and display a tree of all the directories and files beneath it. Be warned, this output can be quite large, as it includes all directories and files under 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 that is too much to list, you can limit the number of levels of files listed by using the -L option followed by the number of levels you want to view:

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

You can also display a tree view of a specific directory:

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

If you use tree to list a large tree, you can combine it with less:

$ tree | less

Again, tree has many other options that you can use, and you can combine them to create even more powerful effects. The 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 find is not available on your system, you can install it using DNF:

$ sudo dnf install findutils

If you run find without any options or parameters, it will recursively list 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 real power of find is that you can search using file names:

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

Or just part of the name -- like a 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 search for files by size. This method may be particularly useful if you are short on space. Now let's list all files larger than 1 MB:

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

Of course, you can also search for a specific directory. Let's say I want to find a file in my Documents folder, 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

Besides files it also displays directories. You can limit the search to only the query files:

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

Once again, find has many more options at your disposal, and if you want to use them, the man page will definitely help you:

$ man find
via: https://fedoramagazine.org/commandline-quick-tips-locate-file/

Summarize

The above is the Linux command line quick trick that I introduced to you, which is how to locate a file. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Connect to Linux command line code example via Python
  • 5 Ways to Send Emails in Linux Command Line (Recommended)
  • Some functions of using tcpdump to capture packets in the Linux command line
  • Two tools for splitting the screen in the Linux command line terminal
  • How to package Android applications through the command line in Linux
  • How to modify IP, DNS and routing command line configuration in Linux
  • How to Communicate with Other Users on the Linux Command Line

<<:  The implementation principle of Vue router-view and router-link

>>:  MySQL sorting Chinese details and examples

Recommend

MySQL 8.0.20 Installation Tutorial with Pictures and Text (Windows 64-bit)

1: Download from mysql official website https://d...

JavaScript imitates Jingdong magnifying glass special effects

This article shares the specific code of JavaScri...

How to build your own Angular component library with DevUI

Table of contents Preface Creating a component li...

MySQL 5.7.16 ZIP package installation and configuration tutorial

This article shares the installation and configur...

How to allow external network access to mysql and modify mysql account password

The root account of mysql, I usually use localhos...

Use semantic tags to write your HTML compatible with IE6,7,8

HTML5 adds more semantic tags, such as header, fo...

Detailed explanation of template tag usage (including summary of usage in Vue)

Table of contents 1. Template tag in HTML5 2. Pro...

Linux command line quick tips: How to locate a file

We all have files stored on our computers -- dire...

Detailed explanation of Vue's hash jump principle

Table of contents The difference between hash and...

How to remount the data disk after initializing the system disk in Linux

Remount the data disk after initializing the syst...

A detailed summary of HTML tag nesting rules suitable for beginners

I have been relearning HTML recently, which can be...

Priority analysis of and or queries in MySQL

This may be an issue that is easily overlooked. F...

Detailed explanation of BOM and DOM in JavaScript

Table of contents BOM (Browser Object Model) 1. W...