mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-22 23:00:57 +08:00
[Translated] 20 Linux ls Command Examples to Display the Entries of Directory
This commit is contained in:
parent
b5542a8435
commit
1ddc413b6e
@ -1,236 +0,0 @@
|
||||
Translating-----------geekpi
|
||||
|
||||
|
||||
20 Linux ls Command Examples to Display the Entries of Directory
|
||||
================================================================================
|
||||
One of the basic command in Linux is ls. Without this command, we may find difficulties to explore the content if directories. This command must known by everyone who want to learn Linux.
|
||||
|
||||
### What is ls ###
|
||||
|
||||
ls command is used to list files and directories. By default, it will be list the content of current directory. With some parameters, we can do more using ls. Here are the samples of ls usage in day-to-day operation
|
||||
|
||||
#### 1. Run ls without parameter ####
|
||||
|
||||
Running ls without parameter will display a list of files and or directories only. No other information can be seen from the output
|
||||
|
||||
$ ls
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls.png)
|
||||
|
||||
#### 2. Use long listing format ####
|
||||
|
||||
Using -l character (small L letter), will display a long listing of the content of current directory. On the next examples, we will combine -l parameter (mostly) to get better result.
|
||||
|
||||
$ ls -l
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_l.png)
|
||||
|
||||
Here’s how to read the output :
|
||||
|
||||
**1st column**
|
||||
The first letter **d** does the content is directory or file. On the screenshot above, Desktop, Documents, Downloads and lynis-1.3.8 are directories. If it - (**minus sign**) it means that the content is file. While if it equal with l (**small L character**), means the content is link file.
|
||||
|
||||
The next 9 character is about file permission. With the **first 3 rwx** characters are for Owner of the file, **the second 3 characters** are for Group owner of the file and **the last 3 characters** are for worldwide access to the file.
|
||||
|
||||
**2nd column**
|
||||
This tell us about how many link to this file
|
||||
|
||||
**3rd column**
|
||||
This tell us about who is the owner of the file / directory
|
||||
|
||||
**4th column**
|
||||
This tell us about who the group owner of the file / directory
|
||||
|
||||
**5th column**
|
||||
This tell us about the size of the file / directory in bytes unit. Except for directories, the size will always count as 4096 bytes
|
||||
|
||||
**6th column**
|
||||
This tell us about the last time and date the file is modified
|
||||
|
||||
**7th column**
|
||||
This tell us the filename or directory name
|
||||
|
||||
#### 3. Show the size of file ####
|
||||
|
||||
Reading size in bytes unit can confuse us. To read 6,5 M is easier compare with reading 6727680 byte. To do this, we can use -hcombine with **-l** parameter. **-h parameter means human readable**
|
||||
|
||||
$ ls -lh
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_lh1.png)
|
||||
|
||||
Another parameter that can do this is **--si** parameter. This parameter is similar with -h parameter, but **--si powers 1000 while -h powers 1024**.
|
||||
|
||||
$ ls -si
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_si.png)
|
||||
|
||||
#### 4. Sort file size ####
|
||||
|
||||
After we can show the file size, we may want to sort it by file size. We can use -S parameter to to this. The list will be sort by the largest file size first.
|
||||
|
||||
$ ls -lhS
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_lhS.png)
|
||||
|
||||
#### 5. Scaling size ####
|
||||
|
||||
Ls can scale size by before printing them using -**-block-size=SIZE**. Where SIZE are :
|
||||
|
||||
K = Kilobyte
|
||||
M = Megabyte
|
||||
G = Gigabyte
|
||||
T = Terabyte
|
||||
P = Petabyte
|
||||
E = Exabyte
|
||||
Z = Zettabyte
|
||||
Y = Yottabyte
|
||||
|
||||
For example, we want to scale size using Megabyte units. So the syntax will be like this :
|
||||
|
||||
$ ls -l --block-size=M
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_block_size.png)
|
||||
|
||||
#### 6. Show hidden files ####
|
||||
|
||||
In Linux, a file begins with “.” (**dot sign**) is a hidden file. To show it on ls command, we can use **-a** parameter.
|
||||
|
||||
$ ls -a
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_a.png)
|
||||
|
||||
#### 7. List directory entries only ####
|
||||
|
||||
If we want to list directory entries only, we can use **-d** parameter.
|
||||
|
||||
$ ls -d */
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_d.png)
|
||||
|
||||
#### 8. Print entries without owner information ####
|
||||
|
||||
To do this, we can use **-g** parameter.
|
||||
|
||||
$ ls -g
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_lg.png)
|
||||
|
||||
#### 9. Print entries without group information ####
|
||||
|
||||
While -g is suppress owner information, **-G** will suppress group information
|
||||
|
||||
$ ls -lG
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_lG2.png)
|
||||
|
||||
#### 10. Print UID and GID ####
|
||||
|
||||
If we want to know the UID and GID of owner and group owner, we can do it using ls command with **-n** parameter. Here’s a sample.
|
||||
|
||||
$ ls -n
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_n.png)
|
||||
|
||||
From the example above, we know that **user pungki has UID** = 1000 and **GID = 1000**. While **root group has GID = 0**
|
||||
|
||||
#### 11. Print with no color ####
|
||||
|
||||
Some Linux distribution is enable color options to ls command. This will make ls print the list in color. If you don’t want it, you can use **--color=never** parameter.
|
||||
|
||||
$ ls --color=never
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_color_never.png)
|
||||
|
||||
#### 12. Print the index number of each file ####
|
||||
|
||||
To print the index number or known as inode number, we can use -i parameter. The index number will appear at first column.
|
||||
|
||||
$ ls -li
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_inode.png)
|
||||
|
||||
#### 13. Add / (slash sign) to mark directory ####
|
||||
|
||||
To do this, use **-p** parameter.
|
||||
|
||||
$ ls -p
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_p.png)
|
||||
|
||||
#### 14. Reverse order while sorting ####
|
||||
|
||||
You may also want to list entries in reverse order. To do this, we can use **-r** parameter.
|
||||
|
||||
$ ls -r
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_r.png)
|
||||
|
||||
#### 15. List subdirectories recursively ####
|
||||
|
||||
With **-R** parameter, you can list directory including its subdirectories.
|
||||
|
||||
$ ls -R
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_Recursive.png)
|
||||
|
||||
#### 16. Sort by extension ####
|
||||
|
||||
You can sort the list by extension using -X parameter or --sort=extension.
|
||||
|
||||
$ ls -lX
|
||||
|
||||
**Or**
|
||||
|
||||
$ ls --sort=extension
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_x.png)
|
||||
|
||||
#### 17. List by modification time ####
|
||||
|
||||
Using -t parameter will sort the list by modification time which the newest first.
|
||||
|
||||
$ ls -lt
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_lt.png)
|
||||
|
||||
#### 18. List your home directory ####
|
||||
|
||||
For listing your home directory, we can short the directory using “~” (**tilde sign**). So you don’t have to type your full directory name. Let say if the home directory name is **/home/pungki**, then **~** sign have the meaning with /home/pungki.
|
||||
|
||||
$ ls ~
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_tilde.png)
|
||||
|
||||
#### 19. List parent directory ####
|
||||
|
||||
Whenever you are inside a directory, you can also list the parent directory without need to type directory full name. Here’s a sample.
|
||||
|
||||
$ ls ../
|
||||
|
||||
This will list the contents of the directory **one** level above.
|
||||
|
||||
$ ls ../../
|
||||
|
||||
This will list the contents of the directory **two** level above.
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_parent.png)
|
||||
|
||||
#### 20. Print the version of ls command ####
|
||||
|
||||
To print it, use --version parameter
|
||||
|
||||
$ ls --version
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_version.png)
|
||||
|
||||
### Conclusion ###
|
||||
|
||||
That’s some parameters which can be used on day-to-day operation. Of course you can always consult with ls manual page by typing **man ls** or **ls --help** on your console to explore more detail.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://linoxide.com/linux-command/linux-ls-command/
|
||||
|
||||
译者:[译者ID](https://github.com/译者ID) 校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
|
@ -0,0 +1,235 @@
|
||||
20个linux ls命令示例来显示目录条目
|
||||
================================================================================
|
||||
Linux中一个基本命令是ls。没有这个命令,我们会在浏览目录条目时会遇到困难。这个命令必须被每个学习Linux的人知道。
|
||||
|
||||
### ls是什么 ###
|
||||
|
||||
ls命令用于列出文件和目录。默认上,他会列出当前目录的内容。带上参数后,我们可以用ls做更多的事情。这里是一些在日常操作中使用到的ls用法的示例。
|
||||
|
||||
#### 1. 不带参数运行ls ####
|
||||
|
||||
不带参数运行ls会只列出文件或者目录。看不大其他信息输出。
|
||||
|
||||
$ ls
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls.png)
|
||||
|
||||
#### 2. 使用长清单模式 ####
|
||||
|
||||
使用-l字符(小写L字符),会显示当前目录内容的常列表。在接下来的例子中,我们会结合-l参数(大多数)来得到更好的结果。
|
||||
|
||||
$ ls -l
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_l.png)
|
||||
|
||||
这里是如何读取输出 :
|
||||
|
||||
**第1列**
|
||||
第一个字母**d**意味着内容是目录或者文件。在上面的截图中,Desktop、 Documents、 Downloads 和 lynis-1.3.8是目录。如果是'-'(**减号**),这意味着它的内容是文件。当它是l(**小写l字符**),意味这内容是链接文件。
|
||||
|
||||
下面的9个字符是关于文件权限。**前3个rwx**字符是文件的拥有者,**第二组3rwx**是文件的所有组,**最后的rwx**是对其他人访问文件的权限。
|
||||
|
||||
**第2列**
|
||||
这行告诉我们有多少链接指向这个文件。
|
||||
|
||||
**第3列**
|
||||
这行告诉我们谁是这个文件/文件夹的所有者。
|
||||
|
||||
**第4列**
|
||||
这行告诉我们谁是这个文件/文件夹的所有组。
|
||||
|
||||
**第5列**
|
||||
这行告诉我们这个文件/文件夹的以B为单位的大小。 除了目录的大小总是4096B。
|
||||
|
||||
**第6列**
|
||||
这告诉我们文件最后的修改时间。
|
||||
|
||||
**第7列**
|
||||
这告诉我们文件名或者目录名。
|
||||
|
||||
#### 3. 显示文件大小 ####
|
||||
|
||||
以B为单位看大小会迷惑我们。6.5M读起来比6727680B更简单。要这么做,我们可以使用-h与**-l**结合的参数。**-h参数意味着人类可读**。
|
||||
|
||||
$ ls -lh
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_lh1.png)
|
||||
|
||||
另外一个可以这么做的参数是**--si**。这个参数和-h参数类似,但是**-si以1000为单位,而-h以1024为单位**。
|
||||
|
||||
$ ls -si
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_si.png)
|
||||
|
||||
#### 4. 排序文件大小 ####
|
||||
|
||||
在我们可以显示文件大小之后,我们希望以文件大小排序。我们可以使用-S参数来这么做。这列表会从大到校排序。
|
||||
|
||||
$ ls -lhS
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_lhS.png)
|
||||
|
||||
#### 5. 测量大小 ####
|
||||
|
||||
ls可以通过使用**-block-size=SIZE**改测量大小。这里的SIZE是:
|
||||
|
||||
K = Kilobyte
|
||||
M = Megabyte
|
||||
G = Gigabyte
|
||||
T = Terabyte
|
||||
P = Petabyte
|
||||
E = Exabyte
|
||||
Z = Zettabyte
|
||||
Y = Yottabyte
|
||||
|
||||
比如,我们希望使用MB作为测量大小。所以语法就会像这样:
|
||||
|
||||
$ ls -l --block-size=M
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_block_size.png)
|
||||
|
||||
#### 6. 显示隐藏文件 ####
|
||||
|
||||
在Linux中,以"."(**点号**)是一个隐藏文件。为了在ls命令中显示它,我们可以使用**-a**选项。
|
||||
|
||||
$ ls -a
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_a.png)
|
||||
|
||||
#### 7. 只列出目录条目 ####
|
||||
|
||||
如果我们希望只列出目录,我们可以使用**-d**选项。
|
||||
|
||||
$ ls -d */
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_d.png)
|
||||
|
||||
#### 8. 不打印拥有者信息 ####
|
||||
|
||||
要这么做,我们使用**-g**选项。
|
||||
|
||||
$ ls -g
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_lg.png)
|
||||
|
||||
#### 9. 不打印组信息 ####
|
||||
|
||||
-g抑制了拥有者信息,**—G**会抑制组信息。
|
||||
|
||||
|
||||
$ ls -lG
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_lG2.png)
|
||||
|
||||
#### 10. 打印UID和GID ####
|
||||
|
||||
如果你想要知道所有人和所有组的UID和GID,我们可以带**-n**选项使用ls命令。这里是个例子。
|
||||
|
||||
$ ls -n
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_n.png)
|
||||
|
||||
从上面的例子中,我们知道**用户pungki的UID**是100,**GID是1000**,而**root组的GID是0**。
|
||||
|
||||
#### 11. 不带颜色打印 ####
|
||||
|
||||
一些Linux发行版已经对ls命令启用彩色。这会使ls以彩色打印列表。如果你不想要这样,你可以使用 **--color=never** 参数。
|
||||
|
||||
$ ls --color=never
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_color_never.png)
|
||||
|
||||
#### 12. 打印每个文件的索引号 ####
|
||||
|
||||
为了打印索引或者众所周知的inode号,我们可以使用-i选项。索引号会显示在第一列。
|
||||
|
||||
$ ls -li
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_inode.png)
|
||||
|
||||
#### 13. 增加 / (斜线) 标记目录 ####
|
||||
|
||||
要这么做,使用**-p选项**。
|
||||
|
||||
$ ls -p
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_p.png)
|
||||
|
||||
#### 14. 排序时反转顺序 ####
|
||||
|
||||
你或许需要在列出条目时反转顺序。要这么做,你可以使用**-r**选项。
|
||||
|
||||
$ ls -r
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_r.png)
|
||||
|
||||
#### 15. 递归列出子目录 ####
|
||||
|
||||
带**-R**参数后,你可以列出包含它子目录的目录。
|
||||
|
||||
$ ls -R
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_Recursive.png)
|
||||
|
||||
#### 16. 扩展名排序 ####
|
||||
|
||||
你可以使用-X参数或者--sort=extension来通过扩展名来排序。
|
||||
|
||||
$ ls -lX
|
||||
|
||||
**或**
|
||||
|
||||
$ ls --sort=extension
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_x.png)
|
||||
|
||||
#### 17. 通过修改时间列出 ####
|
||||
|
||||
使用-t选项会按修改时间排序,新的文件在前。
|
||||
|
||||
$ ls -lt
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_lt.png)
|
||||
|
||||
#### 18. 列出你的家目录 ####
|
||||
|
||||
要列出你的家目录,你可以缩写目录成"~"(**波浪号**)。你不必输入完整的目录名。让我们假设家文件名为**/home/pungki**,那么**波浪号**就对/home/pungki有意义了。
|
||||
|
||||
$ ls ~
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_tilde.png)
|
||||
|
||||
#### 19. 列出父目录 ####
|
||||
|
||||
Whenever you are inside a directory, you can also list the parent directory without need to type directory full name. Here’s a sample.
|
||||
无论你在那个目录,你可以列出父目录而不必输入完整路径。这是个例子。
|
||||
|
||||
$ ls ../
|
||||
|
||||
这回列出**一**层之上的目录内容。
|
||||
|
||||
$ ls ../../
|
||||
|
||||
这回列出**两**层之上的目录内容。
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_parent.png)
|
||||
|
||||
#### 20. 打印ls命令版本 ####
|
||||
|
||||
使用--version参数打印它。
|
||||
|
||||
$ ls --version
|
||||
|
||||
![](http://linoxide.com/wp-content/uploads/2014/01/ls_version.png)
|
||||
|
||||
### 总结 ###
|
||||
|
||||
这些是在日常操作中会使用到的参数。当然你总可以输入**man ls** 或者 **ls --help** 来查询ls的手册页
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://linoxide.com/linux-command/linux-ls-command/
|
||||
|
||||
译者:[geekpi](https://github.com/geekpi) 校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
|
Loading…
Reference in New Issue
Block a user