List One Filename Per Line in Linux
Last Updated :
30 Dec, 2022
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
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
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
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*
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
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.
Similar Reads
How to Find Out File Types in Linux In Linux, everything is considered as a file. In UNIX, seven standard file types are regular, directory, symbolic link, FIFO special, block special, character special, and socket. In Linux/UNIX, we have to deal with different file types to manage them efficiently.Categories of Files in Linux/UNIXIn
7 min read
How to sort lines in text files in Linux | sort Command The sort command in Linux is used to sort a file, arranging the records in a particular order. By default, the sort command sorts file assuming the contents are ASCII. Using options in the sort command can also be used to sort numerically.sort is a command that sorts text files alphabetically, numer
7 min read
Extract Filename From the Full Path in Linux Linux is a family of open-source operating systems and comes as various distributions or distros. The full path in Linux means starting from the root directory "/", the address of the file includes the directories and subdirectories until the file name. A full file path in Linux looks as follows: /h
2 min read
How to Read Text File Into List in Python? In Python, reading a text file into a list is a common task for data processing. Depending on how the file is structuredâwhether it has one item per line, comma-separated values or raw contentâdifferent approaches are available. Below are several methods to read a text file into a Python list using
2 min read
How to Open a File in Linuxâ In Linux, a file is a fundamental unit of storage, representing everything from documents and images to system logs and program data. Unlike traditional operating systems, Linux treats almost everythingâfiles, directories, devices, and processesâas a file. Whether you're accessing a simple text docu
6 min read
Reading Lines by Lines From a File to a Vector in C++ STL Prerequisites: STL in C++Vector in C++File handling in C++ The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, etc. It is a library of container classes, algorithms, and iterators. Vector in C
2 min read