How to list one filename per output line in Linux?

LinuxOperating SystemOpen Source

There are plenty of Linux utility commands present to print the list of all the files and folders that are present in the current directory. Most commonly used are the ls and the find command.

Let’s explore a simple example where we will make use of the ls command to list all the files and the folders present in a particular directory.

Command

ls

Output

api      cluster   docs LICENSE Makefile.generated_files pkg
staging  vendor
build    cmd       go.mod LICENSES _output plugin

On Ubuntu, we get the output in the above format, where all the names of the files and the folders are written like this.

In order to print only the file names in a single line, line by line, we need to either make use of a flag with the ls command or append another command to it.

The first command that will allow us to list one filename per output file is shown below.

Command

ls | tr “” “\n”

In the above command, we are using the tr command that is used for deleting or translating characters.

Output

api
build
CHANGELOG
CHANGELOG.md
cluster
cmd
code-of-conduct.md
CONTRIBUTING.md
docs
go.mod
go.sum
hack
LICENSE
LICENSES
...

Another command that we can use is shown below.

Command

ls -a | cat

Output

api
build
CHANGELOG
CHANGELOG.md
cluster
cmd
code-of-conduct.md
CONTRIBUTING.md
docs
go.mod
go.sum
hack
LICENSE
LICENSES
...

One more command is also there to print the filenames per line.

Command

ls -1

Output

api
build
CHANGELOG
CHANGELOG.md
cluster
cmd
code-of-conduct.md
CONTRIBUTING.md
docs
go.mod
go.sum
hack
LICENSE
LICENSES
...
raja
Published on 29-Jul-2021 11:29:57
Advertisements