Open In App

List One Filename Per Line in Linux

Last Updated : 30 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

One of the most frequent tasks when using the Linux command line is listing the files in a directory. Sometimes we want the list of files to be in a specific format, such as one file per line. ls is a Linux shell command that lists directory contents of files and directories. It is one of the most often used commands in regular Linux/UNIX operations is the ls command. One of the few commands new users learn right away, the command is used to list the contents of a directory.

Some practical examples of the ls command are shown below.  Here we’ll demonstrate how to list one filename per line in this brief tutorial.

Example 1: List One Filename Per Line

There are various ls options that can be used to list the contents of the directory in useful ways, to see all options use can see the help of ls options

$ ls --help

To list one filename per line, we use the ls option -1, so run the following command to list down the items:-

$ ls -1
List one filename per line

 

Example 2: List one filename per line including hidden files

To list hidden files also, we use the -a flag or option in the command. So use this below command to list the directory with hidden files:

$ ls -1a
List one filename per line including hidden files

 

Example 3: List  specific Files Per Line with Flags or Wildcards

Wildcard is a nice utility that we can use with the ls command to list files containing a specific set of strings or patterns. So here are a few examples we can use to list down the items.

1) List files containing a particular extension

Here we will list files containing the .sh extension, use can replace sh with mp3, C, and another extension.

$ ls -1a *.sh
List files containing .sh extension

 

2) List files starting with a specific name

To list files and directories starting with particular alphabets or words we can use this command, here we listed files starting with alphabet ‘t

$ ls -1a t*
List files starting with a specific name

 

Example 4: Choosing an Alternative to a Terminal as the ls Output Target

We can see that the ls command prints the output in the terminal according to the terminal size, and the format adjusts itself according to the terminal size. but if we use the ls with piping the command we can see that ls is giving the output in a different and it’s the default format. To check this we can run the following command

$ ls -a | cat 
 Choosing an Alternative to a Terminal as the ls Output Target

 

The output above shows that each filename miraculously fits in a line.

This is so that the output can be directed to different output destinations, such as a pipe, a terminal, or a redirection. The -1 argument of the ls command will be used by default if the output target is not a terminal. As a result, ls will output the target with one filename per line.

Conclusion

This brief article explained how to list files one per line using the ls command. The key is the -1 option. We have also discovered that the ls command will modify its output based on the various output targets.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads