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

Published on Aug. 22, 2023, 12:17 p.m.

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.

Tags:

ls