tux linux
Before moving to anything more complex and sophisticated, it is crucial to start learning how you can list files in linux to gather information about files and directories both for troubleshooting permission issues, execution problems or just for using them as input to other functions (as we will probably see in another article!).

You can use this command as a base for manipulating files and directories, as I described in another article, Let’s play with files and directories!

ls – A command to list them all!

Again, coreutils package comes in help to our task providing a simple but complete command that allows us to see information about files and directories, by ‘listing’ them in its output.

As you can read in the subtitle, the command is ls and it is used to list files in linux.

Clearly, the sytntax for the command is as follows:

By using it without appending options or arguments the output lists the content, in alphabetical order, of the current directory:

It is also possible to use the [files] argument to show the content of a particular directory:

It’s possible to list multiple directories, by separating them with a space:

Maybe there is something hiding right here!

First option that we are going to consider is the one that allows you to see if there are hidden files in the current location or a particular folder.

Hidden files are recognizable as it is a file whose filename begins with a dot (.) and using the flag -a you can see them, as in the output:

Going detective mode, the -l flag

Now that you are able to list files in linux to see what is in a directory and what is hiding, it’s time to move on and start getting some more information about the content.

With -l flag you will be able to see:

  • File name
  • File permissions
  • File owner
  • File group
  • File size
  • How many hard links the file has
  • File type
  • Date and time

I will use the following sample to go down in detail.

File type

The first character on the left represents the file type, as you can see we got one directory (d) and two files (-).

The most common file types are:

  • “d” – Directory
  • “-“ – Regular file
  • “l” – Symbolic link
  • “s” – Socket

Don’t be confused, directories in linux are actually files, with a particular structure, but still files. I will probably add some more details in a dedicated post.

Permissions and owners

After the file type you can see 9 characters, they describe the permissions of the file (read, write, execution) in the following order:

  • 3 characters for file owner
  • 3 characters for group
  • 3 characters for everyone else

Each character indicates a permission for the given entity, I take the row related to testfile1 as an example:

This line tells you that:

  • It is a file (-)
  • The owner is “alex” and the group that is associated to this file is “alex
  • The owner, alex, can read and write (rw-) the file
  • The group, alex, can only read (r–) the file
  • Everyone else can only read (r–) the file
  • It’s been hardlinked 1 time
  • Its size is 22704 bytes
  • It’s been last modified in jul, 31 @ 13.52

Execution has a different meaning, depending on the file type.

If you are talking about a regular file, it means that the file can be executed, like a script, a command, a program.

If you refer to a directory instead, execution means that you can traverse the directory.

In both cases it does not imply that you can write it.

How to show subdirectories content

You can use the -R flag to show the content of the subdirectories present in the given path, current directory if you use the command without specifying one:

Let’s sort them out!

If you are interested in a particultar sorting order, ls provides you different sorting methods for its output. The default is alphabetical, but you can specify by which field you want to sort out your output.

You can sort output for:

  • File size – using flag –sort=size or -S
  • File modification time – using flag –sort=time or -t
  • File extension, alphabetically – using flag –sort=extension or -X

Appending a -r you get the order reversed.

Seems powerful! Anything else?

Combining the various flags together, this is what you can get from the current directory:

As usual, if you want to go deep in the options available for the command, I suggest you to read the manual for it!

ls – Manual

Now that you know how to list files in linux, the output can be used as input for other commands, can be filtered, can be used as a list to iterate, there are no borders to the usage of the precious information that this command can give you.

I will dedicate different articles on how to use this output, in the meanwhile, if you liked the post, feel free to share it with your friends and colleagues!