1. Overview

The ls command in Linux aids in displaying a directory’s contents. Typically, it lists these files and subdirectories horizontally in the terminal. However, this arrangement depends on the terminal’s width and the number of files present. In detail, once some files occupy the terminal’s width, the remaining ones occupy the next row. This takes place from left to right and continues until all the entries are listed.

In some situations, we might favor showing these contents in a single line. For instance, when we want to improve the readability of the output or prepare this output for manipulation.

In this tutorial, we’ll discuss the different ways we can instruct ls to display its output in a single line.

2. Exploring the -m Option and Command Substitution

Our first approach is pretty straightforward – we simply have to append the -m option to the ls command.

After this, we’ll explore command substitution which is represented by the symbol $(). In detail, it treats a command’s output the same as a variable value. As a result, a different command can use this output as its argument.

We use these two approaches first because they are easy to apply.

First, let’s review the original format of the ls output:

$ ls baeldung
 dir_1  'dir 3'   file_1.txt  'file 3.txt'
 dir_2  'dir 4'   file_2.txt  'file 4.txt'

Above, the contents of the baeldung directory are arranged in the column format.

2.1. Using the -m Option

The -m option modifies the behavior of ls to produce a single-line comma-separated output:

$ ls -m baeldung
dir_1, dir_2, 'dir 3', 'dir 4', file_1.txt, file_2.txt, 'file 3.txt', 'file 4.txt'

In this example, the contents of the baeldung directory are now in a single line. This approach provides a quick way to scan through the contents of a directory.

2.2. Using Command Substitution

When we pass the ls output to the echo command, we can print a single-line output. This echo command is responsible for displaying text or variable values in the terminal:

$ echo $(ls baeldung)
dir_1 dir_2 dir 3 dir 4 file_1.txt file_2.txt file 3.txt file 4.txt

Above, $() allows us to capture the ls output and then pass it to echo as an argument.

It’s crucial to understand that we don’t use any particular delimiters to separate the items.

3. Piping With the ls Command

In Linux, piping facilitates redirecting one command’s output to another command as input.

3.1. Using the tr Command

The tr command substitutes or removes characters from standard input and writes the results to the standard output. We can use it to replace newline characters with whitespace to print a single-line output:

$ ls baeldung | tr '\n' ' '
dir_1 dir_2 dir 3 dir 4 file_1.txt file_2.txt file 3.txt file 4.txt $ 

In the example above, \n represents the newline character while ‘ ‘ represents the space character. The pipe character (|) enables us to pass the output from ls baeldung to tr as input. Then, tr replaces each newline character with the space character.

All the entries are now in a single line but so is the command prompt ($). This is where we type in our commands in the terminal. Therefore, we must add a new line character between the last entry and the prompt. To achieve this, we modify our previous command with the sed command:

$ ls baeldung | tr '\n' ' '| sed 's/.$/\n/'
dir_1 dir_2 dir 3 dir 4 file_1.txt file_2.txt file 3.txt file 4.txt

sed uses regular expressions to find patterns in text and operate on them:

  • s – informs sed that this is a text substitution
  • ‘s/.$/\n/’ – substitutes the last space character in our single-line output with a newline one (\n)

Now we’ve been able to restore the original format of the command prompt. It should exist in its separate line.

3.2. Using ls -i With the paste Command

The paste command’s main role is to merge lines from multiple files. We can combine paste with ls to display output in a single line. Also, this command allows us to specify a delimiter to separate our entries:

$ ls -1 baeldung| paste -sd ","
dir_1,dir_2,dir 3,dir 4,file_1.txt,file_2.txt,file 3.txt,file 4.txt

Using the ls -1 command shows the files and directories vertically. Here, each entry occupies its own line. Then the paste command merges the entries into a single line, separating each with a comma. The -s option concatenates everything into a horizontal output. Meanwhile, the -d option allows us to specify a delimiter which in this case, is a comma.

3.3. Using the awk Command

The awk command is useful since it helps us process text. It carries out this processing based on a pattern:

$ ls -1 baeldung | awk '{ printf $0 ","}'
dir_1,dir_2,dir 3,dir 4,file_1.txt,file_2.txt,file 3.txt,file 4.txt,$

Above, we achieve our objective. However, the command prompt appears on the same line as our output. So, we need to separate it from our output to get a more clear output:

$ ls -1 baeldung | awk '{ printf $0 ","}' | sed 's/.$/\n/'
dir_1,dir_2,dir 3,dir 4,file_1.txt,file_2.txt,file 3.txt,file 4.txt

Additionally, we add the printf command as well as the sed command to further customize our output. We need the printf command to control the appearance of the ls -1 baeldung output. It gives us the flexibility to add spaces, commas, or any other character as a delimiter:

  • ‘{ printf $0 “,”}’ – this is a representation of the pattern in awk
  • printf – formats and prints the value from $0
  • $0 – represents the output from the ls -1 command
  • “,” – adds a comma after each file or directory
  • sed ‘s/.$/\n/’ – substitutes the last value in the output in this case a comma with a newline character. This pushes the command prompt ($) on a different line from the output.

The instruction above helps us display a single-line output of filenames and directories, separated by a comma.

4. Conclusion

In this article, we explored printing the ls output in a single line. We discussed making use of some ls options and command substitution. Also, we explored piping the ls output to other commands for further processing. The command line enables us to efficiently customize our output using multiple commands.

Comments are closed on this article!