ls is a Linux shell command that lists directory contents of files and directories.
- ls syntax
- ls options
- ls examples
- ls code generator
ls syntax
$ ls [options] [file|dir]
ls command options
ls command main options:
ls -a | list all files including hidden file starting with '.' |
ls --color | colored list [=always/never/auto] |
ls -d | list directories - with ' */' |
ls -F | add one char of */=>@| to enteries |
ls -i | list file's inode index number |
ls -l | list with long format - show permissions |
ls -la | list long format including hidden files |
ls -lh | list long format with readable file size |
ls -ls | list with long format with file size |
ls -r | list in reverse order |
ls -R | list recursively directory tree |
ls -s | list file size |
ls -S | sort by file size |
ls -t | sort by time & date |
ls -X | sort by extension name |
ls command examples
You can press the tab button to auto complete the file or folder names.
List directory Documents/Books with relative path:
$ ls Documents/Books
List directory /home/user/Documents/Books with absolute path.
$ ls /home/user/Documents/Books
List root directory:
$ ls /
List parent directory:
$ ls ..
List user's home directory (e.g: /home/user):
$ ls ~
List with long format:
$ ls -l
Show hidden files:
$ ls -a
List with long format and show hidden files:
$ ls -la
Sort by date/time:
$ ls -t
Sort by file size:
$ ls -S
List all subdirectories:
$ ls *
Recursive directory tree list:
$ ls -R
List only text files with wildcard:
$ ls *.txt
ls redirection to output file:
$ ls > out.txt
List directories only:
$ ls -d */
List files and directories with full path:
$ ls -d $PWD/*
ls code generator
Select ls options and press the Generate Code button:
See also
ls command is one of the most commonly used tools in Unix. You simply cannot underestimate the importance of being able to confirm exactly what files and directories are available to you, and ls does its job perfectly. Most of recent Linux and Unix distros support ls colorized output which is shown above.
ls – basic syntax
Like many commands in Unix, ls expects you to specify a file or directory name for it to inspect:
$ ls file1 dir2This command above will list (check if it exists) file called file1, and will also show contents of the dir2 directory (if dir2 exists).
The most basic way to use ls command is to simply make it list files and directories in your current directory. You don’t need to specify any parameters for it, just type the command itself.
In this example below, I’m in ~/proj directory (~ sign simply means my home directory, so with homedir /home/greys the ~/proj means /home/greys/proj) where some of my Linux-based projects are:
[email protected]:~/proj $ ls ansible bash glebreys.com gleb.reys.net python unixtutorial unlocker writingAll of these names – ansible, bash, glebreys.com, etc are the directories in that /home/greys/proj directory.
ls – most common usage
If you’re interested in a particular file or directory, you should specify the filename as a parameter to ls.
This command will simply output the filename of the file you have specified:
$ ls /etc/passwd /etc/passwdSimilarly, it’s possible to confirm that a certain file is not found:
$ ls /etc/passwd5 ls: /etc/passwd5: No such file or directoryYou may also specify a directory (the output in this example is abridged) to confirm the contents of it:
$ ls /etc Muttrc Net X11 adduser.conf adjtime aliases aliases.db alternatives apache2 apm apt at.deny ...And finally, the most common way ls is used: the long version of the output, which is invoked using the -l parameter:
$ ls -l /etc total 1064 -rw-r--r-- 1 root root 8063 Mar 8 2007 Muttrc -rw-r--r-- 1 root root 611 Mar 5 2007 Net drwxr-xr-x 5 root root 4096 Sep 7 04:44 X11 -rw-r--r-- 1 root root 2077 Aug 3 2006 adduser.conf -rw-r--r-- 1 root root 44 Aug 3 2006 adjtime -rw-r--r-- 1 root root 51 Mar 25 2007 aliases -rw-r--r-- 1 root root 12288 Sep 7 05:01 aliases.db drwxr-xr-x 2 root root 4096 Sep 7 05:03 alternatives drwxr-xr-x 8 root root 4096 Sep 26 03:02 apache2 drwxr-xr-x 6 root root 4096 Aug 3 2006 apm drwxr-xr-x 4 root root 4096 Sep 7 04:34 apt -rw-r----- 1 root root 144 Aug 3 2006 at.denySymbolic links can be inspected to show destination files using pretty standard ls -l combination:
List SELinux Contexts with ls
ls command can be used with the -Z parameter to show file contexts in SELinux enabled systems:
Such output allows you to confirm the type of each directory entry, the access permissions, the number of links to this file, the ownership (user and unix group which own it), size in bytes, date of the last modification, and, finally, the name of the directory entry.
See also
The ls command is used to list files. "ls" on its own lists all files in the current directory except for hidden files. "ls *.tex" lists only those files ending in ".tex". There are a large number of options; here are some of the most useful. Options can be combined (this is a general principle of Unix commands) - for example "ls -la" gives a long listing of all files.
- ls -a will list all files including hidden files (files with names beginning with a dot).
- ls -F gives a full listing, indicating what type files are by putting a slash after directories and a star after executable files (programs you can run).
- ls -l gives a long listing of all files. Here is an example section of the output of ls -l :
drwxr-xr-x 6 eva users 1024 Jun 8 16:46 sabon -rw------- 1 eva users 1564 Apr 28 14:35 splus -rw------- 1 eva users 1119 Apr 28 16:00 splus2 -rw-r--r-- 1 eva users 9753 Sep 27 11:14 ssh_known_hosts -rw-r--r-- 1 eva users 4131 Sep 21 15:23 swlist.out -rw-r--r-- 1 eva users 94031 Sep 1 16:07 tarnti.zipWhat does it all mean?
- The first column gives the type of the file (e.g., directory or ordinary file) and the file permissions.
- The second column is the number of links to the file i.e., (more or less) the number of names there are for the file. Generally an ordinary file will only have one link, but a directory will have more, because you can refer to it as ``dirname'', ``dirname/.'' where the dot means ``current directory'', and if it has a subdirectory named ``subdir'', ``dirname/subdir/..'' (the ``..'' means ``parent directory'').
- The third and fourth columns are the user who owns the file and the Unix group of users to which the file belongs. Unless you are working together on the same file, you need not worry about Unix groups.
- The fifth column is the size of the file in bytes.
- The next three columns are the time at which the file was last changed (for a directory, this is the time at which a file in that directory was last created or deleted).
- The last column is the name of the file.
- ls -R gives a recursive listing, including the contents of all subdirectories and their subdirectories and so on.
- ls -t lists the files in order of the time when they were last modified (newest first) rather than in alphabetical order.
- ls -r lists the files in the reverse of the order that they would otherwise have been listed in. Thus, ls -lrt will give a long listing, oldest first, which is handy for seeing which files in a large directory have recently been changed.
In Linux, the ls command is one of the fundamental tools. It lists files and directories with or without various additional information. The ls command is a part of the GNU core utility package. It should be available on any Linux distro.
This guide will showcase how to use the ls command, specifically “ls -l” in combination with other options.
Linux ls command
The ls command takes the location of a directory and prints all the files and directories within the location. It can also print additional file information like file permissions, file ownership, file size, etc.
Command structure
This is the command structure that all ls commands must follow.
$ ls <options> <directory>
If no directory is specified, then ls performs its action on the current directory. For example, the following command will list all the files and directories (name only).
Listing files and directories in long list format
By default, the ls command will print the name of all the files and directories only. To get the additional information and a cleaner view, use the flag “-l”.
In long list format, ls shows the following info about each file.
- File type
- File permissions
- Hard links
- Ownership
- Group
- Size
- Date and time
Additional options
There are numerous options available to modify the output of the ls command. All the following options are combined with “ls -l” to use the long list format by default.
Showing backslash after directories
By default, the ls command uses coloring to distinguish between files and directories. However, it may not be reliable in various situations; for example, you’re accessing through a console that doesn’t support coloring.
In such a situation, use the flag “-F” to show a backslash “/” after the name of a directory.
Sorting
The ls command will show the list in a specific order (generally by file name in alphabetic order). However, it also supports sorting based on other values.
To sort the output in reverse order, use the flag “-r”.
To sort the files and directories by time and date of creation/modification, use the flag “-t” instead.
We can also sort the output alphabetically by entry extension. To do so, use the flag “-X”.
Showing hidden files
By default, the ls command won’t show hidden files. These are the files that start with “.” at the beginning of the file name.
To show the hidden files, use the flag “-a”.
Directory tree
A directory tree shows the hierarchy of the target directory and its sub-directories and files. Generally, we use the tree command to check the directory tree.
However, the ls command can also showcase the directory tree (although not-so-good-looking). To print the tree view, use the flag “-R”.
Showing inode number
In Linux, each file has its unique inode value. Inode stores metadata for every single file on the partition. These data are stored at the start of each partition. It stores all the file info except the file name and the data itself.
To show the inode value of each file and directories, use the flag “-i”.
Showing UID and GID
In Linux, each file has its UID (unique identifier) and GID (group ID). To show the UID and GID of the files, use the flag “-n”.
Human-readable format
By default, the ls command prints file size in bytes. We can tell to print all the values in human-readable format for easier understanding of them.
More options
These are only a handful of options that the ls command supports. For the full list of supported options as well as detailed explanations, check out the man page.
Final thoughts
In this guide, we learned about the ls command in Linux. It’s one of the fundamental tools available on all Linux distros. This guide showcases many common methods of using the ls command. Once mastered, it can be more convenient to use it than a GUI file browser.
Happy computing!