mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-03-24 02:20:09 +08:00
commit
0e4c865dde
@ -1,3 +1,5 @@
|
||||
theo-l translating
|
||||
|
||||
10 Useful ‘ls’ Command Interview Questions – Part 2
|
||||
================================================================================
|
||||
Continuing the legacy of ls command here is the second interview article on Listing command. The first article of the series was highly appreciated by the Tecmint Community. If you’ve missed the first part of this series you may like to visit at:
|
||||
@ -158,10 +160,10 @@ That’s all for now. We will be coming up with the next part of this article se
|
||||
via: http://www.tecmint.com/ls-interview-questions/
|
||||
|
||||
作者:[Ravi Saive][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
译者:[theo-l](https://github.com/theo-l)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.tecmint.com/author/admin/
|
||||
[1]:http://www.tecmint.com/ls-command-interview-questions/
|
||||
[1]:http://www.tecmint.com/ls-command-interview-questions/
|
||||
|
@ -11,7 +11,7 @@ Linux ls Command
|
||||
ls with no option list files and directories in bare format where we won’t be able to view details like file types, size, modified date and time, permission and links etc.
|
||||
|
||||
# ls
|
||||
|
||||
|
||||
0001.pcap Desktop Downloads index.html install.log.syslog Pictures Templates
|
||||
anaconda-ks.cfg Documents fbcmd_update.php install.log Music Public Videos
|
||||
|
||||
@ -20,7 +20,7 @@ ls with no option list files and directories in bare format where we won’t be
|
||||
Here, ls -l (-l is character not one) shows file or directory, size, modified date and time, file or folder name and owner of file and it’s permission.
|
||||
|
||||
# ls -l
|
||||
|
||||
|
||||
total 176
|
||||
-rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap
|
||||
-rw-------. 1 root root 1586 Jul 31 02:17 anaconda-ks.cfg
|
||||
@ -42,7 +42,7 @@ Here, ls -l (-l is character not one) shows file or directory, size, modified da
|
||||
List all files including hidden file starting with ‘.‘.
|
||||
|
||||
# ls -a
|
||||
|
||||
|
||||
. .bashrc Documents .gconfd install.log .nautilus .pulse-cookie
|
||||
.. .cache Downloads .gnome2 install.log.syslog .netstat.swp .recently-used.xbel
|
||||
0001.pcap .config .elinks .gnome2_private .kde .opera .spice-vdagent
|
||||
@ -56,7 +56,7 @@ List all files including hidden file starting with ‘.‘.
|
||||
With combination of -lh option, shows sizes in human readable format.
|
||||
|
||||
# ls -lh
|
||||
|
||||
|
||||
total 176K
|
||||
-rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap
|
||||
-rw-------. 1 root root 1.6K Jul 31 02:17 anaconda-ks.cfg
|
||||
@ -78,7 +78,7 @@ With combination of -lh option, shows sizes in human readable format.
|
||||
Using -F option with ls command, will add the ‘/’ Character at the end each directory.
|
||||
|
||||
# ls -F
|
||||
|
||||
|
||||
0001.pcap Desktop/ Downloads/ index.html install.log.syslog Pictures/ Templates/
|
||||
anaconda-ks.cfg Documents/ fbcmd_update.php install.log Music/ Public/ Videos/
|
||||
|
||||
@ -87,7 +87,7 @@ Using -F option with ls command, will add the ‘/’ Character at the end each
|
||||
The following command with ls -r option display files and directories in reverse order.
|
||||
|
||||
# ls -r
|
||||
|
||||
|
||||
Videos Public Music install.log fbcmd_update.php Documents anaconda-ks.cfg
|
||||
Templates Pictures install.log.syslog index.html Downloads Desktop 0001.pcap
|
||||
|
||||
@ -96,21 +96,21 @@ The following command with ls -r option display files and directories in reverse
|
||||
ls -R option will list very long listing directory trees. See an example of output of the command.
|
||||
|
||||
# ls -R
|
||||
|
||||
|
||||
total 1384
|
||||
-rw-------. 1 root root 33408 Aug 8 17:25 anaconda.log
|
||||
-rw-------. 1 root root 30508 Aug 8 17:25 anaconda.program.log
|
||||
|
||||
|
||||
./httpd:
|
||||
total 132
|
||||
-rw-r--r-- 1 root root 0 Aug 19 03:14 access_log
|
||||
-rw-r--r--. 1 root root 61916 Aug 10 17:55 access_log-20120812
|
||||
|
||||
|
||||
./lighttpd:
|
||||
total 68
|
||||
-rw-r--r-- 1 lighttpd lighttpd 7858 Aug 21 15:26 access.log
|
||||
-rw-r--r--. 1 lighttpd lighttpd 37531 Aug 17 18:21 access.log-20120819
|
||||
|
||||
|
||||
./nginx:
|
||||
total 12
|
||||
-rw-r--r--. 1 root root 0 Aug 12 03:17 access.log
|
||||
@ -121,7 +121,7 @@ ls -R option will list very long listing directory trees. See an example of outp
|
||||
With combination of -ltr will shows latest modification file or directory date as last.
|
||||
|
||||
# ls -ltr
|
||||
|
||||
|
||||
total 176
|
||||
-rw-r--r--. 1 root root 11439 Jul 31 02:13 install.log.syslog
|
||||
-rw-r--r--. 1 root root 48867 Jul 31 02:17 install.log
|
||||
@ -143,7 +143,7 @@ With combination of -ltr will shows latest modification file or directory date a
|
||||
With combination of -lS displays file size in order, will display big in size first.
|
||||
|
||||
# ls -lS
|
||||
|
||||
|
||||
total 176
|
||||
-rw-r--r--. 1 root root 48867 Jul 31 02:17 install.log
|
||||
-rw-r--r--. 1 root root 46701 Jul 31 09:58 index.html
|
||||
@ -165,7 +165,7 @@ With combination of -lS displays file size in order, will display big in size fi
|
||||
We can see some number printed before file / directory name. With -i options list file / directory with inode number.
|
||||
|
||||
# ls -i
|
||||
|
||||
|
||||
20112 0001.pcap 23610 Documents 23793 index.html 23611 Music 23597 Templates
|
||||
23564 anaconda-ks.cfg 23595 Downloads 22 install.log 23612 Pictures 23613 Videos
|
||||
23594 Desktop 23585 fbcmd_update.php 35 install.log.syslog 23601 Public
|
||||
@ -175,7 +175,7 @@ We can see some number printed before file / directory name. With -i options lis
|
||||
Check version of ls command.
|
||||
|
||||
# ls --version
|
||||
|
||||
|
||||
ls (GNU coreutils) 8.4
|
||||
Copyright (C) 2010 Free Software Foundation, Inc.
|
||||
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
|
||||
@ -188,7 +188,7 @@ Check version of ls command.
|
||||
List help page of ls command with their option.
|
||||
|
||||
# ls --help
|
||||
|
||||
|
||||
Usage: ls [OPTION]... [FILE]...
|
||||
|
||||
### 13. List Directory Information ###
|
||||
@ -210,7 +210,7 @@ With ls -l command list files under directory /tmp. Wherein with -ld parameters
|
||||
----------
|
||||
|
||||
# ls -ld /tmp/
|
||||
|
||||
|
||||
drwxrwxrwt. 13 root root 4096 Aug 21 12:48 /tmp/
|
||||
|
||||
### 14. Display UID and GID of Files ###
|
||||
@ -218,7 +218,7 @@ With ls -l command list files under directory /tmp. Wherein with -ld parameters
|
||||
To display UID and GID of files and directories. use option -n with ls command.
|
||||
|
||||
# ls -n
|
||||
|
||||
|
||||
total 36
|
||||
drwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Downloads
|
||||
drwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Music
|
||||
@ -235,7 +235,7 @@ We have made alias for ls command, when we execute ls command it’ll take -l op
|
||||
Note: We can see number of alias available in your system with below alias command and same can be unalias as shown below example.
|
||||
|
||||
# alias
|
||||
|
||||
|
||||
alias cp='cp -i'
|
||||
alias l.='ls -d .* --color=auto'
|
||||
alias ll='ls -l --color=auto'
|
||||
|
@ -1,189 +0,0 @@
|
||||
theo-l translating
|
||||
|
||||
15 Interview Questions on Linux “ls” Command – Part 1
|
||||
================================================================================
|
||||
The listing command in UNIX and UNIX like operating System ‘ls‘ is one of the most basic and widely used utility in command-line. It is POSIX compliant utility available for GNU coreutils and BSD variants. The ‘ls’ command can be used with a variety of options to get desired results. This article aims at deep insight of file listing command in Linux with relevant examples.
|
||||
|
||||

|
||||
15 ls Command Questions
|
||||
|
||||
### 1. How will you List files from a directory? ###
|
||||
|
||||
Ans: The Linux file listing command ‘ls‘ comes to rescue here.
|
||||
|
||||
# ls
|
||||
|
||||

|
||||
List Files
|
||||
|
||||
Alternatively, we can use ‘echo‘ command to list files within a directory in association with wildcard (*).
|
||||
|
||||
# echo *
|
||||
|
||||

|
||||
List All Files
|
||||
|
||||
### 2. How will you list all the directories only using echo command? ###
|
||||
|
||||
# echo */
|
||||
|
||||

|
||||
List All Directories
|
||||
|
||||
### 3. How will you list all the files within a directory including hidden files aka (.) dot files? ###
|
||||
|
||||
Ans: We need to use option ‘-a‘ (list hidden files) with command ‘ls‘.
|
||||
|
||||
# ls -a
|
||||
|
||||

|
||||
List All Hidden Files
|
||||
|
||||
### 4. How do you list all the files within a directory including hidden files, but do not list implied ‘.’ and ‘..’? ###
|
||||
|
||||
Ans: We need to use option ‘-A‘ (do not list implied . and ..) with command ‘ls‘.
|
||||
|
||||
# ls -A
|
||||
|
||||

|
||||
Do Not List Implied
|
||||
|
||||
### 5. How will you print the content of a directory in long format listing? ###
|
||||
|
||||
Ans: We need to use option ‘l‘ (long format) with command ‘ls‘.
|
||||
|
||||
# ls -l
|
||||
|
||||

|
||||
List Files Long
|
||||
|
||||
In the above example, the output seems like.
|
||||
|
||||
drwxr-xr-x 5 avi tecmint 4096 Sep 30 11:31 Binary
|
||||
|
||||
Here, drwxr-xr-x is file permission for owner, group and world. Owner has Read(r), Write(w) and Execute(x) permission. The group to which this file belongs has Read(r) and Execute(x) permission but not Write(w) permission, same permission implies for the world that have access to this file.
|
||||
|
||||
- The Initial ‘d‘ means its a Directory.
|
||||
- Number ‘5‘ represents Symbolic Link.
|
||||
- The File Binary belongs to user avi and group tecmint.
|
||||
- Sep 30 11:31 represents the date and time it was last modified.
|
||||
|
||||
### 6. You are supposed to print the content of directory in long format listing, showing hidden/dot files. How will you achieve this? ###
|
||||
|
||||
Ans: We need to use option ‘-a‘ (list hidden files) and ‘-l‘ (long listing) together with command ‘ls‘.
|
||||
|
||||
# ls -la
|
||||
|
||||

|
||||
Print Content of Directory
|
||||
|
||||
Alternatively We can use option ‘-A‘ and ‘-l‘ with ‘ls‘ command, if we do not want to list implied ‘.’ and ‘..’.
|
||||
|
||||
# ls -lA
|
||||
|
||||
### 7. How will you figure out the author of each file? ###
|
||||
|
||||
Ans: We need to use option ‘–author‘ along with option ‘-l‘ to print the author name of each file.
|
||||
|
||||
# ls --author -l
|
||||
|
||||

|
||||
List Author Files
|
||||
|
||||
### 8. How will you print escape for non-graphic character? ###
|
||||
|
||||
Ans: We just need to use option ‘-b‘ to print escape for non-graphic character.
|
||||
|
||||
# ls -b
|
||||
|
||||

|
||||
|
||||
### 9. List the size of files and folders in desired scale format. How will you achieve this? ###
|
||||
|
||||
Ans: Here option ‘–block-size=scale‘ along with option ‘-l‘ needs to be used. We need to remove ‘scale’ in the example with the desired scale viz M, K, etc.
|
||||
|
||||
# ls --block-size=M -l
|
||||
# ls --block-size=K -l
|
||||
|
||||

|
||||
List File Scale Format
|
||||
|
||||
### 10. List the files within a directory, but don’t show the backup files, i.e., those files that end with ~. ###
|
||||
|
||||
Ans: Here option ‘-B‘ (do not list implied entries ending with ~) comes to rescue.
|
||||
|
||||
# ls -B
|
||||
|
||||

|
||||
List File Without Backup
|
||||
|
||||
### 11. Sort all the files within a directory by name and show associated last modification information. ###
|
||||
|
||||
Ans: We need to use option ‘-c‘ and option ‘-l‘ with command ls to fulfil the need as suggested above.
|
||||
|
||||
# ls -cl
|
||||
|
||||

|
||||
Sort Files
|
||||
|
||||
### 12. Sort all the files within a directory by modification time and show associated information. ###
|
||||
|
||||
Ans: We need to use three options together i.e., ‘-l‘, ‘-t‘ and ‘-c‘ with command ls to sort files by modification time, newest first.
|
||||
|
||||
# ls -ltc
|
||||
|
||||

|
||||
Sort Files by Modification
|
||||
|
||||
### 13. How will you control the output of ‘ls’ command to be colorful or no-color? ###
|
||||
|
||||
Ans: We need to use option ‘–color=parameter‘. The parameter to be used with color option are ‘auto’, ‘always’ and ‘never’ which are self explanatory.
|
||||
|
||||
# ls --color=never
|
||||
# ls --color=auto
|
||||
# ls --color=always
|
||||
|
||||

|
||||
ls Colorful Output
|
||||
|
||||
### 14. You are supposed to list directory entries themselves, not their contents. What will you do? ###
|
||||
|
||||
Ans: Here the option ‘-d‘ comes handy.
|
||||
|
||||
# ls -d
|
||||
|
||||

|
||||
List Directory Entries
|
||||
|
||||
### 15. Create an alias for long format listing “ls -l” as “ll” and output the result to a file and not standard output. ###
|
||||
|
||||
Ans: Here in the above scenario, we need to add alias to .bashrc file and then use redirect operator to write the output to file and not standard output. We will be using editor nano.
|
||||
|
||||
# ls -a
|
||||
# nano .bashrc
|
||||
# ll >> ll.txt
|
||||
# nano ll.txt
|
||||
|
||||

|
||||
Create Alias for ls command
|
||||
|
||||
That’s all for now. Don’t forget to provide us with your valuable feedback in the comments below. I’ll be here again with another interesting article soon. Till then stay tuned and connected.
|
||||
|
||||
### Real Also: ###
|
||||
|
||||
- [10 ‘ls’ Command Interview Questions – Part 2][1]
|
||||
- [15 Basic ‘ls’ Commands in Linux][2]
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.tecmint.com/ls-command-interview-questions/
|
||||
|
||||
作者:[Avishek Kumar][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.tecmint.com/author/avishek/
|
||||
[1]:http://www.tecmint.com/ls-interview-questions/
|
||||
[2]:http://www.tecmint.com/15-basic-ls-command-examples-in-linux/
|
@ -0,0 +1,190 @@
|
||||
关于linux中的“ls”命令的15个面试问题 - 第一部分
|
||||
================================================================================
|
||||
Unix或类Unix系统中的“文件列表”命令“ls”是最基础并且使用的最广泛的命令行中工具之一。
|
||||
它是一个在GNU基本工具集以及BSD各种变体上可用的与POSIX兼容的工具。
|
||||
“ls”命令可以通过与大量的选项一起使用来达到想要的结果。
|
||||
这篇文章的目的在于通过相关的样例来深入讨论文件列表命令。
|
||||
|
||||

|
||||
15个“ls”命令问题。
|
||||
|
||||
### 1. 你会如何从目录中列出文件?###
|
||||
|
||||
答:使用linux文件列表命令“ls”驾到拯救。
|
||||
|
||||
# ls
|
||||
|
||||

|
||||
列出文件
|
||||
|
||||
同时,我们也可以使用“echo(打印)”命令与一个通配符(*)相关联的方式在目录中列出其中的所有文件。
|
||||
|
||||
# echo *
|
||||
|
||||

|
||||
列出所有的文件。
|
||||
|
||||
### 2. 你会如何只通过使用echo命令来列出目录中的所有文件?###
|
||||
|
||||
# echo */
|
||||
|
||||

|
||||
列出所有的目录
|
||||
|
||||
### 3. 你会怎样列出一个目录中的所有文件, 包括隐藏的dot文件?###
|
||||
|
||||
答:我们需要将“-a”选项与“ls”命令一起使用。
|
||||
|
||||
# ls -a
|
||||
|
||||

|
||||
列出所有的隐藏文件。
|
||||
|
||||
### 4. 如何列出目录中除了 “当前目录暗喻(.)”和“父目录暗喻(..)”之外的所有文件,包括隐藏文件?###
|
||||
|
||||
答: 我们需要将“-A”选项与“ls”命令一起使用
|
||||
|
||||
# ls -A
|
||||
|
||||

|
||||
别列出暗喻文件。
|
||||
|
||||
### 5. 如何将当前目录中的内容使用长格式打印列表?###
|
||||
|
||||
答: 我们需要将“-l”选项与“ls”命令一起使用。
|
||||
|
||||
# ls -l
|
||||
|
||||

|
||||
列出文件的长格式。
|
||||
|
||||
上面的样例中,其输出结果看起来向下面这样。
|
||||
|
||||
drwxr-xr-x 5 avi tecmint 4096 Sep 30 11:31 Binary
|
||||
|
||||
上面的drwxr-xr-x 是文件的权限,分别代表了文件所有者,组以及对整个世界。 所有者具有读(r),写(w)以及执行(x)等权限。 该文件所属组具有读(r)和执行(x)但是没有写的权限,相同的权限预示着
|
||||
对于整个世界的其他可以访问该文件的用户。
|
||||
|
||||
- 开头的‘d’意味着这是一个目录
|
||||
- 数字'5'表示符号链接
|
||||
- 文件 Binary归属于用户 “avi”以及用户组 "tecmint"
|
||||
- Sep 30 11:31 表示文件最后一次的访问日期与时间。
|
||||
|
||||
### 6. 假如让你来将目录中的内容以长格式列表打印,并且显示出隐藏的“点文件”,你会如何实现?###
|
||||
|
||||
答: 我们需要同时将"-a"和"-l"选项与“ls”命令一起使用。
|
||||
|
||||
# ls -la
|
||||
|
||||

|
||||
打印目录内容
|
||||
|
||||
同时,如果我们不想列出“当前目录暗喻”和"父目录暗喻",可以将“-A”和“-l”选项同“ls”命令一起使用。
|
||||
|
||||
# ls -lA
|
||||
|
||||
### 7. 如何找到每个文件的创建者?###
|
||||
|
||||
答: 我们需要结合 “--author”和 "-l"选项来打印出每个文件的创建者。
|
||||
|
||||
# ls --author -l
|
||||
|
||||

|
||||
列出文件创建者。
|
||||
|
||||
### 8. 如何对非显示字符进行转义打印?###
|
||||
|
||||
答:我们只需要使用“-b”选项来对非显示字符进行转义打印
|
||||
|
||||
# ls -b
|
||||
|
||||

|
||||
|
||||
### 9. 指定特定的单位格式来列出文件和目录的大小,你会如何实现?###
|
||||
答: 在此可以同时使用选项“-block-size=scale”和“-l”,但是我们需要用特定的单位如M,K等来替换‘scale’。
|
||||
|
||||
# ls --block-size=M -l
|
||||
# ls --block-size=K -l
|
||||
|
||||

|
||||
列出文件大小单位格式。
|
||||
|
||||
### 10. 列出目录中的非备份文件,也就是那些文件名以‘~’结尾的文件###
|
||||
|
||||
答: 选项‘-B’赶来救驾。
|
||||
|
||||
# ls -B
|
||||
|
||||

|
||||
列出非备份文件
|
||||
|
||||
### 11. 将目录中的所有文件按照名称进行排序并与最后修改时间信息进行关联显示###
|
||||
|
||||
答: 为了实现这个需求,我们需要同时将“-c”和"-l"选项与命令一起使用。
|
||||
|
||||
# ls -cl
|
||||
|
||||

|
||||
文件排序
|
||||
|
||||
### 12. 将目录中的文件按照修改时间进行排序,并显示相关联的信息。###
|
||||
|
||||
答: 我们需要同时使用3个选项--'-l','-t','-c'--与命令‘ls’一起使用来对文件使用修改时间排序,最新的修改时间排在最前。
|
||||
|
||||
# ls -ltc
|
||||
|
||||

|
||||
按照修改时间对文件排序。
|
||||
|
||||
### 13. 如何控制‘ls’命令的输出颜色的有无?###
|
||||
|
||||
答: 需要使用选项‘--color=parameter’,parameter参数值具有三种不同值,“auto(自动)”,“always(一直)”,“never(无色)”。
|
||||
|
||||
# ls --color=never
|
||||
# ls --color=auto
|
||||
# ls --color=always
|
||||
|
||||

|
||||
ls的输出颜色
|
||||
|
||||
### 14. 假如只需要列出目录本身,而不是目录的内容,你会如何做?###
|
||||
|
||||
答:在此“-d”选项就会显得很顺手。
|
||||
|
||||
# ls -d
|
||||
|
||||

|
||||
列出目录本身
|
||||
|
||||
### 15. 为长格式列表命令"ls -l"创建别名“ll”,并将其结果输出到一个文件而不是标准输出中。###
|
||||
|
||||
答:在上述的这个场景中,我们需要将别名添加到.bashrc文件中,然后使用重定向操作符将输出写入到文件而不是标准输出中。我们将会使用编辑器nano。
|
||||
|
||||
# ls -a
|
||||
# nano .bashrc
|
||||
# ll >> ll.txt
|
||||
# nano ll.txt
|
||||
|
||||

|
||||
为ls命令创建别名。
|
||||
|
||||
先到此为止,别忘了在下面的评论中提出你们的宝贵意见,我会再次带着另外的有趣的文章在此闪亮登场。
|
||||
|
||||
### 参考阅读:###
|
||||
|
||||
- [10 个‘ls’命令的面试问题-第二部分][1]
|
||||
- [Linux中15个基础的'ls'命令][2]
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.tecmint.com/ls-command-interview-questions/
|
||||
|
||||
作者:[Avishek Kumar][a]
|
||||
译者:[theo-l](https://github.com/theo-l)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.tecmint.com/author/avishek/
|
||||
[1]:http://www.tecmint.com/ls-interview-questions/
|
||||
[2]:http://www.tecmint.com/15-basic-ls-command-examples-in-linux/
|
Loading…
Reference in New Issue
Block a user