ls is a Linux shell command that lists directory contents of files and directories. Show
ls syntax$ ls [options] [file|dir] ls command optionsls command main options:
ls command examplesYou 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 generatorSelect 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 syntaxLike many commands in Unix, ls expects you to specify a file or directory name for it to inspect: This 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 usageIf 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 lsls 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.
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 commandThe 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 structureThis 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 formatBy 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.
Additional optionsThere 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 directoriesBy 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. SortingThe 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 filesBy 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 treeA 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 numberIn 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 GIDIn 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 formatBy 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 optionsThese 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 thoughtsIn 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! |