mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-22 23:00:57 +08:00
transalted
This commit is contained in:
parent
d90d0e710e
commit
9f9b54e071
@ -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](http://www.tecmint.com/wp-content/uploads/2014/09/ls-Command-Questions.png)
|
||||
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](http://www.tecmint.com/wp-content/uploads/2014/09/list-files.gif)
|
||||
List Files
|
||||
|
||||
Alternatively, we can use ‘echo‘ command to list files within a directory in association with wildcard (*).
|
||||
|
||||
# echo *
|
||||
|
||||
![List All Files](http://www.tecmint.com/wp-content/uploads/2014/09/list-all-files.gif)
|
||||
List All Files
|
||||
|
||||
### 2. How will you list all the directories only using echo command? ###
|
||||
|
||||
# echo */
|
||||
|
||||
![List All Directories](http://www.tecmint.com/wp-content/uploads/2014/09/list-all-directories.gif)
|
||||
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](http://www.tecmint.com/wp-content/uploads/2014/09/list-all-hidden-files.gif)
|
||||
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](http://www.tecmint.com/wp-content/uploads/2014/09/Do-not-list-Implied.gif)
|
||||
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](http://www.tecmint.com/wp-content/uploads/2014/09/list-files-long.gif)
|
||||
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](http://www.tecmint.com/wp-content/uploads/2014/09/Print-Content-of-Directory.gif)
|
||||
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](http://www.tecmint.com/wp-content/uploads/2014/09/List-Author-Files.gif)
|
||||
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
|
||||
|
||||
![Print Escape Character](http://www.tecmint.com/wp-content/uploads/2014/09/Print-Escape-Character.gif)
|
||||
|
||||
### 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](http://www.tecmint.com/wp-content/uploads/2014/09/List-File-Scale-Format.gif)
|
||||
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](http://www.tecmint.com/wp-content/uploads/2014/09/List-File-Without-Backup.gif)
|
||||
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](http://www.tecmint.com/wp-content/uploads/2014/09/Sort-Files.gif)
|
||||
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](http://www.tecmint.com/wp-content/uploads/2014/09/Sort-Files-by-Modification.gif)
|
||||
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](http://www.tecmint.com/wp-content/uploads/2014/09/ls-colorful-output.gif)
|
||||
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](http://www.tecmint.com/wp-content/uploads/2014/09/List-Directory-Entries.gif)
|
||||
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](http://www.tecmint.com/wp-content/uploads/2014/09/Create-ls-Alias.gif)
|
||||
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,183 @@
|
||||
关于linux中的“ls”命令的15个面试问题 - 第一部分
|
||||
================================================================================
|
||||
Unix或类Unix系统中的“文件列表”命令“ls”是最基础并且使用的最广泛的命令行中工具之一。
|
||||
它是一个在GNU基本工具集以及BSD各种变体上可用的与POSIX兼容的工具。
|
||||
“ls”命令可以通过与大量的选项一起使用来达到想要的结果。
|
||||
这篇文章的目的在于通过相关的样例来深入讨论文件列表命令。
|
||||
|
||||
![15 ls Command Questions](http://www.tecmint.com/wp-content/uploads/2014/09/ls-Command-Questions.png)
|
||||
15个“ls”命令问题。
|
||||
|
||||
### 1. 你会如何从目录中列出文件?###
|
||||
|
||||
答:使用linux文件列表命令“ls”驾到拯救。
|
||||
|
||||
# ls
|
||||
|
||||
![List Files](http://www.tecmint.com/wp-content/uploads/2014/09/list-files.gif)
|
||||
列出文件
|
||||
|
||||
同时,我们也可以使用“echo(打印)”命令与一个通配符(*)相关联的方式在目录中列出其中的所有文件。
|
||||
# echo *
|
||||
|
||||
![List All Files](http://www.tecmint.com/wp-content/uploads/2014/09/list-all-files.gif)
|
||||
列出所有的文件。
|
||||
|
||||
### 2. 你会如何只通过使用echo命令来列出目录中的所有文件?###
|
||||
# echo */
|
||||
|
||||
![List All Directories](http://www.tecmint.com/wp-content/uploads/2014/09/list-all-directories.gif)
|
||||
列出所有的目录
|
||||
|
||||
### 3. 你会怎样列出一个目录中的所有文件, 包括隐藏的dot文件?###
|
||||
|
||||
答:我们需要将“-a”选项与“ls”命令一起使用。
|
||||
# ls -a
|
||||
|
||||
![List All Hidden Files](http://www.tecmint.com/wp-content/uploads/2014/09/list-all-hidden-files.gif)
|
||||
列出所有的隐藏文件。
|
||||
|
||||
### 4. 如何列出目录中除了 “当前目录暗喻(.)”和“父目录暗喻(..)”之外的所有文件,包括隐藏文件?###
|
||||
|
||||
答: 我们需要将“-A”选项与“ls”命令一起使用
|
||||
# ls -A
|
||||
|
||||
![Do Not List Implied](http://www.tecmint.com/wp-content/uploads/2014/09/Do-not-list-Implied.gif)
|
||||
别列出暗喻文件。
|
||||
|
||||
### 5. 如何将当前目录中的内容使用长格式打印列表?###
|
||||
|
||||
答: 我们需要将“-l”选项与“ls”命令一起使用。
|
||||
# ls -l
|
||||
|
||||
![List Files Long](http://www.tecmint.com/wp-content/uploads/2014/09/list-files-long.gif)
|
||||
列出文件的长格式。
|
||||
|
||||
上面的样例中,其输出结果看起来向下面这样。
|
||||
|
||||
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
|
||||
|
||||
![Print Content of Directory](http://www.tecmint.com/wp-content/uploads/2014/09/Print-Content-of-Directory.gif)
|
||||
打印目录内容
|
||||
|
||||
同时,如果我们不想列出“当前目录暗喻”和"父目录暗喻",可以将“-A”和“-l”选项同“ls”命令一起使用。
|
||||
# ls -lA
|
||||
|
||||
### 7. 如何找到每个文件的创建者?###
|
||||
|
||||
答: 我们需要结合 “--author”和 "-l"选项来打印出每个文件的创建者。
|
||||
# ls --author -l
|
||||
|
||||
![List Author Files](http://www.tecmint.com/wp-content/uploads/2014/09/List-Author-Files.gif)
|
||||
列出文件创建者。
|
||||
|
||||
### 8. 如何对非显示字符进行转义打印?###
|
||||
|
||||
答:我们只需要使用“-b”选项来对非显示字符进行转义打印
|
||||
|
||||
# ls -b
|
||||
|
||||
![Print Escape Character](http://www.tecmint.com/wp-content/uploads/2014/09/Print-Escape-Character.gif)
|
||||
|
||||
### 9. 指定特定的单位格式来列出文件和目录的大小,你会如何实现?###
|
||||
答: 在此可以同时使用选项“-block-size=scale”和“-l”,但是我们需要用特定的单位如M,K等来替换‘scale’。
|
||||
|
||||
# ls --block-size=M -l
|
||||
# ls --block-size=K -l
|
||||
|
||||
![List File Scale Format](http://www.tecmint.com/wp-content/uploads/2014/09/List-File-Scale-Format.gif)
|
||||
列出文件大小单位格式。
|
||||
|
||||
### 10. 列出目录中的非备份文件,也就是那些文件名以‘~’结尾的文件###
|
||||
|
||||
答: 选项‘-B’赶来救驾。
|
||||
|
||||
# ls -B
|
||||
|
||||
![List File Without Backup](http://www.tecmint.com/wp-content/uploads/2014/09/List-File-Without-Backup.gif)
|
||||
列出非备份文件
|
||||
|
||||
### 11. 将目录中的所有文件按照名称进行排序并与最后修改时间信息进行关联显示###
|
||||
|
||||
答: 为了实现这个需求,我们需要同时将“-c”和"-l"选项与命令一起使用。
|
||||
|
||||
# ls -cl
|
||||
|
||||
![Sort Files](http://www.tecmint.com/wp-content/uploads/2014/09/Sort-Files.gif)
|
||||
文件排序
|
||||
|
||||
### 12. 将目录中的文件按照修改时间进行排序,并显示相关联的信息。###
|
||||
|
||||
答: 我们需要同时使用3个选项--'-l','-t','-c'--与命令‘ls’一起使用来对文件使用修改时间排序,最新的修改时间排在最前。
|
||||
|
||||
# ls -ltc
|
||||
|
||||
![Sort Files by Modification](http://www.tecmint.com/wp-content/uploads/2014/09/Sort-Files-by-Modification.gif)
|
||||
按照修改时间对文件排序。
|
||||
|
||||
### 13. 如何控制‘ls’命令的输出颜色的有无?###
|
||||
|
||||
答: 需要使用选项‘--color=parameter’,parameter参数值具有三种不同值,“auto(自动)”,“always(一直)”,“never(无色)”。
|
||||
|
||||
# ls --color=never
|
||||
# ls --color=auto
|
||||
# ls --color=always
|
||||
|
||||
![ls Colorful Output](http://www.tecmint.com/wp-content/uploads/2014/09/ls-colorful-output.gif)
|
||||
ls的输出颜色
|
||||
|
||||
### 14. 假如只需要列出目录本身,而不是目录的内容,你会如何做?###
|
||||
|
||||
答:在此“-d”选项就会显得很顺手。
|
||||
|
||||
# ls -d
|
||||
|
||||
![List Directory Entries](http://www.tecmint.com/wp-content/uploads/2014/09/List-Directory-Entries.gif)
|
||||
列出目录本身
|
||||
|
||||
### 15. 为长格式列表命令"ls -l"创建别名“ll”,并将其结果输出到一个文件而不是标准输出中。###
|
||||
|
||||
答:在上述的这个场景中,我们需要将别名添加到.bashrc文件中,然后使用重定向操作符将输出写入到文件而不是标准输出中。我们将会使用编辑器nano。
|
||||
|
||||
# ls -a
|
||||
# nano .bashrc
|
||||
# ll >> ll.txt
|
||||
# nano ll.txt
|
||||
|
||||
![Create Alias for ls command](http://www.tecmint.com/wp-content/uploads/2014/09/Create-ls-Alias.gif)
|
||||
为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