Skip to main content

ls Command in Linux

Updated 9 min readMarkdown
Part of seriesLinux

After you understand the cd command here, you also need to understand the ls command. It stands for list and is basically to show the list of contents in a directory. For further usage, it can be customised to show the other information as well as to filter information as you need.

Because the function is to show the contents, you have to ensure the directory that you want to list the contents is the correct location. Use the pwd command to know where you are as explained in this part.

⚠️ WARNING:

Kindly note that ls command doesn't open any file/directory.

ls versus search app

There are so many fancy GUI search app to list the contents as the ls command does. Why do we still need the ls command?

You need to understand that search is different than listing using ls command, although they share common results as follows.

Aspect ls command Search app
Context Informs what it is (i.e. permissions, size, modified time, filename, extension, ownership, links, config files, sort by xxx) immediately and very lightweight. Can touch hidden files. Primarily informs the location, but some apps add more info (i.e. modified date, size, filename, extension, sort by xxx). Some search apps run in the background (indexing) to speed up searching time which adds another load (RAM) even during idle and consuming storage.
Unity of commands Easy to integrate with the other commands or to follow another command without needing to leave the Terminal. The more familiar you are, the faster your typing. You can search fastly, but you need another function to move, copy, delete, or duplicate. Some functions can only be done in Terminal.
Network capability Remote servers usually have no GUI. A big issue when remote server needs to use a search app.
Aid kit Can still run in any situation. Sometimes cannot run in recovery mode, or a worse situation.

From the explanation above, it is crystal clear that command line interface (CLI) is NOT a primitive interface. If you look at Github, GitLab, Bitbucket, OneDev, Gogs, Codeberg, Radicle, Gitea, and other development platforms, you will find many programs have no GUI (yet), but powerful.

Basic command

Basic command of ls is very simple:

ls

To show the contents of the other directory, without needing to go into it (using cd command) the syntax is:

ls [option] [directory/file name + path]

⚠️ WARNING: The space matters. Use quote marks (" " or ' ') or backslash (/) to write filename containing space. Read more here.

Detailed view

ls -l

The -l asks the linux to show the contents of a directory in a long listing format (detail view). Here, the "l" stands for long. The other requests can be added after it, for example -lh, -la, or both -lah. Any requests behind the -l are usually interchangeable because these commands give the same results:

ls -lah
ls -hal
ls -ahl
ls -hla
ls -l -a -h
ls -l -h -a
ls -a -h -l
ls -a -l -h
ls -h -a -l
ls -h -l -a

⚠️ WARNING: Some command have both short and long versions, but some only have the short version. The ls command is case-sensitive.

Examples:

Short version Long version
-l Does not exist
-a --al
-h --human-readable

To see the complete list of -ls showing which commands have both short and long versions, use this command:

man ls

If you get a difficulty read in Terminal, save it in a TXT format, then open it using a text editor. To save it in a TXT file, use this command:

man ls > ls_arguments.txt

Sample results of the basic command:

total 246160
-rw-r--r--  1 mylinux admin         4 Jan 18 06:05 debian-binary
drwxr-xr-x  2 mylinux admin      4096 Mar 27 17:00 Desktop
drwxr-xr-x  9 mylinux admin      4096 Jun  8 05:35 Documents
drwxr-xr-x  4 mylinux admin      1016 Sep 14 20:18 Downloads

📝 Info:

  • The total refers only to the contents currently being listed, not the entire drive. For example, the "total 246160" means roughly 240 Mb.

  • In general, the information resulted by ls that you have to understand is:

    • filetype: d = folder; - = file

    • permissions

    • size

    • modified date

    • filename

Column 1 - Permissions

The first column from left is the permission. drwxr-xr-x or -rw-r--r-- is file type (the first character) and permissions (the other 9 characters.

Meaning of the first character:

  • d means directory.

  • l means symbolic link.

  • c means character device.

  • b means block device.

  • s means socket.

  • p means pipe.

Meaning of the 9 characters will be easier if we divide them into 3 groups. For example, rwxr-xr-x is split into:

Group

Contents

Applies to

First group

rwx

Owner

Second group

r-x

Group

Third group

r-x

Everyone else

Meaning of permissions in the 9 characters (second character to the last character):

  • r means can read.

  • w means can write/modify/delete.

  • x means can execute/access/run.

  • - means there is no permission.

⚠️ WARNING: File permission can be changed or lost when a file is leaving the Linux machine where that file was created. To preserve permissions, you'd better to use TAR compression before sending a file.

In Linux, filenames are not the actual file itself. A filename is more like a label, or a pointer, which is pointing to data stored somewhere on the disk. Linux allows multiple filenames which point to the same underlying file data. Those pointers are called hard links.

Imagine the real file is a house, filenames are addresses pointing to that house. You can have multiple addresses, leading to the same house. For example:

  • A house with pink gate and little pony on the grass

  • 789 ABC St.

  • Linux Building, Greenland

The numbers in the column 2 (1, 2, 9, 4) show how many filename point to each file/directory.

However, hord line of a directory is different because it is related to subdirectories. Rule of thumb is 2 + number of subdirectories. For example, if the hard link of a directory is 2, the directory has no subdirectory. The 2 here because each directory automatically has:

.
..

Single dot is the current directory, while double dots mean the parent directory. To check how many hard links of a file/directory, use this command:

ls -ld [file/directory name and full path]

If you want to create another link to a file/directory without creating another one, try ln command. It is a standard Unix/Linux terminal utility used to create links between files and directories. It allows you to access the same data from different locations without creating actual copies, which saves disk space. Basic syntax:

ln [TARGET] [LINK_NAME]

📝 Info:

  • [TARGET] is the existing file/directory.

  • [LINK_NAME] is the name of the new link.

Other than hard link, there is a soft link (often called symlink or symbolic link). It acts like a "shortcut" in Windows. It is a special file that points to the path of the original file or directory. If the original is deleted, the symlink "breaks" and becomes invalid. Whereas, in the hard link, if you delete the original file/directory, the data remains accessible through the hard link until all names for that data are deleted.

Column 3 - Owner

The mylinux is the example of owner/user who is the controller of the file/directory. The creator usually and automatically becomes the owner of the file/directory which allows the strongest permissions (to read and modify a file/directory), while the others in default cannot.

Column 4 - Group

The linux is the example of group which is a collection of users. Groups allow shared access.

Analogy of owner and group is like at the office. The person who owns the file cabinet is the owner, while the other colleagues can access it. The owner can give different permission to his/her colleagues. For instance, accounting guys just to read the files, while production guys can read and write the files.

Groups matter because it allows collaboration, shared folders, controlled access, multi-user systems which is very important on servers, universities, companies. However, the group does not matter for a single-user desktop computer.

Column 5 - Size

Usually the size is in bytes. If the file size is very big (i.e. 38594016427), the more digits make it difficult to read. Read more on Human-Readable View command below.

Column 6 to 8 - Modified time

It is the modified time. If a file/directory is older, Linux may show year instead of time.

Column 9 - Filename

Filename of file (including the extension) or directory.

Human-Readable View

ls -lh

will show a list with human-readble filesize like 13M, 452K.

Show hidden files/directories

All files and directories begin with dot (.) will be hidden. They are usually configuration files. To show them in ls command:

ls -a

📝 Info: You can combine the -a with the other command like -h, -l.

Sort by time

The sort rule is the newest file at the top.

ls -lt

Sort by size

The biggest filesize at the top.

ls -lS

⚠️ WARNING: The S is capital.

📝 Info: These commands give the same results:

ls -lSh
ls -Slh

Recursive listing

This command will be valuable with the other sorting commands (i.e. sort by size or by time).

ls -Slhr

will give results with the smallest filesize at the top of a human-readable list.

ls -tlhr

will give results with the oldest file at the top of a human-readable list.

Show filesize and name only

ls -sh

Filtering list

Use asterisk (*) to filter by extension or filename.

To filter mp4 files only:

ls *.mp4

To filter pdf files only:

ls *.pdf

To show all files begin with your keyword:

ls [keyword]*

📝 Info: Powerfull filtering can use grep or find.

Grouped results

ls -R

All files will be showed under their parents.

About the author

  • Safety professional by day, Linux explorer by night.

No comments yet. Be the first to comment.