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: 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: 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:
|
<<: The implementation principle of Vue router-view and router-link
>>: MySQL sorting Chinese details and examples
Overview MySQL also has its own event scheduler, ...
Preface This article introduces the fifth questio...
This article uses an example to share with you a ...
I spent a day on it before this. Although Seata i...
Table of contents 1. Functional description 2. Pa...
Table of contents ReactHooks Preface WhyHooks? Fo...
Table of contents 1 redis configuration file 2 Do...
1. After installing MySQL 5.6, it cannot be enabl...
I recently watched Rich Harris's <Rethinki...
Table of contents mysql log files binlog Binlog l...
Table of contents What is Docker Compose Requirem...
According to Chinese custom, we are still celebra...
Table of contents 1. View the storage engine of t...
Preface This article mainly introduces the releva...
Table of contents Preface 1. Introduction to one-...