mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
[手动选题][tech]: 20230704.0 ⭐️⭐️ Using ls Command in Linux.md
This commit is contained in:
parent
4c8882394e
commit
5c30f57edb
258
sources/tech/20230704.0 ⭐️⭐️ Using ls Command in Linux.md
Normal file
258
sources/tech/20230704.0 ⭐️⭐️ Using ls Command in Linux.md
Normal file
@ -0,0 +1,258 @@
|
||||
[#]: subject: "Using ls Command in Linux"
|
||||
[#]: via: "https://itsfoss.com/ls-command/"
|
||||
[#]: author: "Sagar Sharma https://itsfoss.com/author/sagar/"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: " "
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
Using ls Command in Linux
|
||||
======
|
||||
|
||||
In my opinion, the ls command is the [most used Linux command][1] as it is often used to verify the outcome of the previous operation by [listing the directory contents][2].
|
||||
|
||||
The ls command stands for a list; all it does is list the contents of the specified directory.
|
||||
|
||||
In this tutorial, I'll walk you through multiple examples of using the ls command.
|
||||
|
||||
I will also share some practice questions to test the learnings.
|
||||
|
||||
### How to use the ls command
|
||||
|
||||
To use the ls command, you'd have to follow the simple command syntax:
|
||||
|
||||
```
|
||||
ls [OPTIONS] Targeted_Directory
|
||||
```
|
||||
|
||||
Here,
|
||||
|
||||
- `[OPTIONS]`: It is used to tweak the default behavior of the ls command.
|
||||
- `Targeted_Directory`: This is where you provide the directory name or the absolute path to the directory.
|
||||
|
||||
You might wonder what happens when you use the ls command without any options. And to answer that, I used the ls command in my current working directory:
|
||||
|
||||
```
|
||||
ls
|
||||
```
|
||||
|
||||
![use the ls command to list the contents of the working directory][3]
|
||||
|
||||
As you can see, it listed all the directories and files available in the current working directory. But you can do a lot more than just getting the names and files present in the current working directory.
|
||||
|
||||
Let me show you how.
|
||||
|
||||
#### 1. List files and directories with ownership
|
||||
|
||||
One of the primary use of the ls command to find the permissions and ownership of a particular file or a directory.
|
||||
|
||||
For that you'd have to use the `-l` option (also called long listing) with the ls command:
|
||||
|
||||
```
|
||||
ls -l
|
||||
```
|
||||
|
||||
Once you do that, you can expect similar output:
|
||||
|
||||
```
|
||||
[email protected]:~$ ls -lh
|
||||
-rwxrw-r-- 1 sagar sagar 666M Dec 10 18:16 Fedora.iso
|
||||
```
|
||||
|
||||
Notice how I used an addition `-h` option? I'll discuss it in the next section.
|
||||
|
||||
For now, if the output looks too complicated, then let me simplify things for you:
|
||||
|
||||
![explain read write permissions with file ownership in Linux using the ls command][4]
|
||||
|
||||
As you can see, each text block has its own meaning such as the permissions for owner, group, and others.
|
||||
|
||||
If you want to dive deep into file permissions, I would recommend [our detailed guide on file permissions in Linux:][5]
|
||||
|
||||
#### 2. Get information in a human-readable format
|
||||
|
||||
By default, the file size is shown in bytes which is not the best way to know your file size. So how do you get the same info but in human-readable form?
|
||||
|
||||
Simple. You use the `-h` option with the ls command:
|
||||
|
||||
```
|
||||
ls -l -h
|
||||
```
|
||||
|
||||
And here's the comparison between the default and the human-readable form:
|
||||
|
||||
![list files using the ls command in human readable format][6]
|
||||
|
||||
Much better. Isn't it?
|
||||
|
||||
> 💡 The ls command is good to see the file sizes. However, it won't give you the directory size which is almost always displayed as 4K. To get directory size, use the du command.
|
||||
|
||||
#### 3. List hidden files
|
||||
|
||||
Like any other file manager, the ls command won't list the hidden files (I mean they are supposed to be hidden. Right?).
|
||||
|
||||
But what if you want to [list the hidden files][7] along with the regular files? To do so, you can use the `-a` option:
|
||||
|
||||
```
|
||||
ls -a
|
||||
```
|
||||
|
||||
As you can see, the filename starting with a dot `.` are hidden files.
|
||||
|
||||
![list hidden files in Linux terminal using the ls command][8]
|
||||
|
||||
> 💡 You may also use`ls -A`which works almost the same as`ls -a`except it won't include the`.`and`..`directories.
|
||||
|
||||
#### 4. List files recursively
|
||||
|
||||
There are [multiple ways to list files recursively][9] and using the ls command is one of them.
|
||||
|
||||
In case you don't know, listing files recursively means listing files of all the sub-directories present until the last element of every sub-directory is shown.
|
||||
|
||||
And to list files recursively, you can use the `-R` flag as shown:
|
||||
|
||||
```
|
||||
ls -R
|
||||
```
|
||||
|
||||
![list files recursilvely in Linux using the ls command][10]
|
||||
|
||||
In a way, it gives you the current directory structure. Personally, I prefer the for this purpose but you'll have to install it first.
|
||||
|
||||
> 💡 You don't have to be in the directory to list its content. You can also list directory contents by providing its absolute or relative path like this:`ls /var/log`
|
||||
|
||||
#### 5. Differentiate between files and directories while using ls
|
||||
|
||||
While the different colors for files and directories should do the job. But due to some reason, if you want to symbolize files and directories here you have it.
|
||||
|
||||
In the ls command, you have a `-F` flag that adds a forward slash `/` to every directory name:
|
||||
|
||||
```
|
||||
ls -F
|
||||
```
|
||||
|
||||
![differenciate between files and directories while using the ls command][11]
|
||||
|
||||
#### 6. List only files with certain file extensions
|
||||
|
||||
There are times when you only want to list files with specific file extensions and trust me this is the easiest of all.
|
||||
|
||||
To do that, you don't have to use any options. Just append the file extension to the Asterisk `*` such as `*.png`, `*.txt`, etc:
|
||||
|
||||
```
|
||||
ls *.extension
|
||||
```
|
||||
|
||||
For example, if I only want to list the ISO files, then, I will be using the following command:
|
||||
|
||||
```
|
||||
ls *.iso
|
||||
```
|
||||
|
||||
![list files of a specific file extensions][12]
|
||||
|
||||
#### 7. Sort output based on size
|
||||
|
||||
To sort the output based on the file size, you'd have to use the `-S` flag and it will list files from largest to the smallest manner (descending):
|
||||
|
||||
```
|
||||
ls -lhS
|
||||
```
|
||||
|
||||
![sort files based on their file size using the ls command][13]
|
||||
|
||||
Similarly, if you want to reverse this order to list the smallest files first, you can use the `-r` flag to reverse the order:
|
||||
|
||||
```
|
||||
ls -lhSr
|
||||
```
|
||||
|
||||
![sort files from smallest to largest using the ls command][14]
|
||||
|
||||
#### 8. Sort files based on date and time
|
||||
|
||||
The ls commands includes the modified time in its listing.
|
||||
|
||||
To list the newest files first, you can use the `-t` flag as shown:
|
||||
|
||||
```
|
||||
ls -lht
|
||||
```
|
||||
|
||||
![show newest files first while using the ls command][15]
|
||||
|
||||
You can use the `-r` flag as I explained previously to reverse the order here too.
|
||||
|
||||
```
|
||||
ls -lrt
|
||||
```
|
||||
|
||||
This will give you the latest modified files at the bottom of the display. This is particularly helpful if you have too many files in the directory and want to see which files were modified recently. I used this while troubleshooting my software project.
|
||||
|
||||
![Using ls -lrt command][16]
|
||||
|
||||
### Let's summarize what you've learned so far!
|
||||
|
||||
Here, I will share a table with multiple options that were used with the ls command in this tutorial:
|
||||
|
||||
| Command | Description |
|
||||
| :- | :- |
|
||||
| `ls -l` | Long list of files and directories |
|
||||
| `ls -lh` | Prints information in human-readable form |
|
||||
| `ls -a` | Include hidden files in listing |
|
||||
| `ls -R` | List files recursively |
|
||||
| `ls -F` | Add a forward slash to the directory name |
|
||||
| `ls *.ext` | List files having specific extensions |
|
||||
| `ls -lS` | Sort files based on file size |
|
||||
| `ls -lt` | Sort files based on time |
|
||||
| `-r` | Reverse the sorting (combined with S or t) |
|
||||
|
||||
### 🏋️ And practice your learning
|
||||
|
||||
Practicing what you've learned is always a good idea, which is why we try to add a practice section in each terminal guide.
|
||||
|
||||
So here are some simple practice exercise of the ls command:
|
||||
|
||||
- List the contents of the `/var/log`
|
||||
- [Save the command output][17] in a file named output.txt
|
||||
- Identify the 3 most recent files (Use time based sorting)
|
||||
- Display the files based on their size but in reverse order
|
||||
- Check if there are any hidden files
|
||||
|
||||
That would be good practice for you. Stay tuned for more Linux command learning.
|
||||
|
||||
And if you are new to the terminal, don't forget to follow our Terminal Basics series
|
||||
|
||||
Enjoy :)
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/ls-command/
|
||||
|
||||
作者:[Sagar Sharma][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/sagar/
|
||||
[b]: https://github.com/lkxed/
|
||||
[1]: https://itsfoss.com/essential-ubuntu-commands/
|
||||
[2]: https://itsfoss.com/list-directory-content/
|
||||
[3]: https://itsfoss.com/content/images/2023/07/use-the-ls-command-to-list-the-contents-of-the-working-directory.png
|
||||
[4]: https://itsfoss.com/content/images/2023/07/explain-read-write-permissions-in-linux-filesystem.png
|
||||
[5]: https://linuxhandbook.com:443/linux-file-permissions/
|
||||
[6]: https://itsfoss.com/content/images/2023/07/list-files-using-the-ls-command-in-human-readable-format.png
|
||||
[7]: https://itsfoss.com/show-hidden-files-linux/
|
||||
[8]: https://itsfoss.com/content/images/2023/07/list-hidden-files-in-Linux-terminal-using-the-ls-command.png
|
||||
[9]: https://linuxhandbook.com:443/list-files-recursively/
|
||||
[10]: https://itsfoss.com/content/images/2023/07/list-files-recursilvely-in-Linux-using-the-ls-command.png
|
||||
[11]: https://itsfoss.com/content/images/2023/07/differenciate-between-files-and-directories-while-using-the-ls-command.png
|
||||
[12]: https://itsfoss.com/content/images/2023/07/list-files-of-a-specific-file-extensions.png
|
||||
[13]: https://itsfoss.com/content/images/2023/07/sort-files-based-on-their-file-size-using-the-ls-command.png
|
||||
[14]: https://itsfoss.com/content/images/2023/07/sort-files-from-smallest-to-largest-using-the-ls-command.png
|
||||
[15]: https://itsfoss.com/content/images/2023/07/show-newest-files-first-while-using-the-ls-command.png
|
||||
[16]: https://itsfoss.com/content/images/2023/07/output_for_ls_-lrt-1.png
|
||||
[17]: https://itsfoss.com/save-command-output-to-file-linux/
|
Loading…
Reference in New Issue
Block a user