Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to list one filename per output line in Linux?
There are several Linux commands available to list files and directories. By default, the ls command displays output in multiple columns across the terminal width. However, there are various methods to format the output so that each filename appears on a separate line, which is useful for scripting and better readability.
Default ls Command Behavior
The standard ls command displays files and directories in a multi-column format:
ls
api cluster docs LICENSE Makefile.generated_files pkg staging vendor build cmd go.mod LICENSES _output plugin
This default behavior can make it difficult to process filenames individually, especially in scripts or when you need to count files.
Methods to List One Filename Per Line
Using the -1 Flag
The most straightforward method is using the -1 (dash one) flag with the ls command:
ls -1
api build CHANGELOG CHANGELOG.md cluster cmd code-of-conduct.md CONTRIBUTING.md docs go.mod go.sum hack LICENSE LICENSES
This flag forces ls to display one entry per line, regardless of terminal width.
Using tr Command for Character Translation
You can pipe the output of ls to the tr command to replace spaces with newlines:
ls | tr " " "<br>"
api build CHANGELOG CHANGELOG.md cluster cmd code-of-conduct.md CONTRIBUTING.md docs go.mod go.sum hack LICENSE LICENSES
The tr command translates or deletes characters. Here, it replaces each space character with a newline character.
Combining ls with cat
Another approach involves using cat with the -a flag:
ls -a | cat
. .. api build CHANGELOG CHANGELOG.md cluster cmd code-of-conduct.md CONTRIBUTING.md docs go.mod
Note: The -a flag includes hidden files (those starting with a dot), including the current directory (.) and parent directory (..).
Comparison of Methods
| Method | Command | Shows Hidden Files | Best For |
|---|---|---|---|
| Direct flag | ls -1 | No | Simple listing, scripts |
| Character translation | ls | tr " " " " |
No | Learning command chaining |
| With hidden files | ls -a | cat | Yes | Complete directory listing |
| All files, one per line | ls -1a | Yes | Comprehensive file listing |
Additional Useful Variations
You can combine flags for more specific output:
ls -1a # One per line including hidden files ls -1l # One per line with detailed information ls -1t # One per line sorted by modification time
Conclusion
The ls -1 command is the most efficient way to list one filename per line in Linux. While alternative methods using tr or cat achieve similar results, the -1 flag is specifically designed for this purpose and provides the cleanest solution for scripts and manual file management.
