深度阅读

How to display only file names using the ls command in Linux?

作者
作者
2023年08月22日
更新时间
10.82 分钟
阅读时间
0
阅读量

To display only the file names in the output of the ls command in Linux, you can use the -1 (one) option, which will list one file per line. Here is an example command that will display only the names of the files in the current directory:

ls -1

This command will output a list of the file names in the current directory, with each file name on a separate line.

Alternatively, you can use the -p (slash) option to append a slash to the end of the directory names, which can help distinguish them from regular files. Here is an example command that will display only the file and directory names in the current directory:

ls -p | grep -v /

This command will output a list of the file and directory names in the current directory, with the directory names followed by a slash.

Note that if a file or directory name contains a new line character, it will be displayed on a separate line regardless of whether you use the -1 option. In this case, you may need to use other tools, such as find, to display the file names in a more reliable format.

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。