Linux and Unix ls command
Lists the contents of a directory.
ls [-a] [-A] [-b] [-c] [-C] [-d] [-f] [-F] [-g] [-i] [-l] [-L] [-m] [-o] [-p] [-q] [-r] [-R] [-s] [-t] [-u] [-x] [pathnames]
|-a||Shows you all files, even files that are hidden (these files begin with a dot.)|
|-A||List all files including the hidden files. However, does not display the working directory (.) or the parent directory (..).|
|-b||Force printing of non-printable characters to be in octal \ddd notation.|
|-c||Use time of last modification of the i-node (file created, mode changed, and so forth) for sorting (-t) or printing (-l or -n).|
|-C||Multi-column output with entries sorted down the columns. Generally this is the default option.|
|-d||If an argument is a directory it only lists its name not its contents.|
|-f||Force each argument to be interpreted as a directory and list the name found in each slot. This option turns off -l, -t, -s, and -r, and turns on -a; the order is the order in which entries appear in the directory.|
|-F||Mark directories with a trailing slash (/), doors with a trailing greater-than sign (>), executable files with a trailing asterisk (*), FIFOs with a trailing vertical bar (|), symbolic links with a trailing at-sign (@), and AF_Unix address family sockets with a trailing equals sign (=).|
|-g||Same as -l except the owner is not printed.|
|-i||For each file, print the i-node number in the first column of the report.|
|-l||Shows you huge amounts of information (permissions, owners, size, and when last modified.)|
|-L||If an argument is a symbolic link, list the file or directory the link references rather than the link itself.|
|-m||Stream output format; files are listed across the page, separated by commas.|
|-n||The same as -l, except that the owner's UID and group's GID numbers are printed, rather than the associated character strings.|
|-o||The same as -l, except that the group is not printed.|
|-p||Displays a slash ( / ) in front of all directories.|
|-q||Force printing of non-printable characters in file names as the character question mark (?).|
|-r||Reverses the order of how the files are displayed.|
|-R||Includes the contents of subdirectories.|
|-s||Give size in blocks, including indirect blocks, for each entry.|
|-t||Shows you the files in modification time.|
|-u||Use time of last access instead of last modification for sorting (with the -t option) or printing (with the -l option).|
|-x||Displays files in columns.|
|-1||Print one entry per line of output.|
|pathnames||File or directory to list.|
In the above example this command would list each of the files in the current directory and the files permissions, the size of the file, date of the last modification, and the file name or directory. Below is additional information about each of the fields this command lists.
|Permissions||Directories||Group||Size||Date||Directory or file|
|drwx------||2||users||4096||Nov 2 19:51||mail/|
|drwxr-s---||35||www||32768||Jan 20 22:39||public_html/|
|-rw-------||1||users||3||Nov 25 02:58||test.txt|
Below is a brief description of each of the above categories shown when using the ls -l command.
Permissions - The permissions of the directory or file.
Directories - The amount of links or directories within the directory. The default amount of directories is going to always be 2 because of the . and .. directories.
Group - The group assigned to the file or directory
Size - Size of the file or directory.
Date - Date of last modification.
Directory of file - The name of the file or file.
Our favorite ls command, which lists files with permissions, shows hidden files, displays in a column format, and doesn't show the group.
ls -1 | wc -l
Count how many files and directories are in the current directory. To prevent any confusion, the above command reads ls <dash><the #1> <pipe> wc <dash><the letter l>. This command uses the ls command to list files in a bare format and pipes the output into the wc command to count how many files are listed. When done properly, the terminal should return a single number indicating how many lines were counted and then return you to the prompt.
Tip: Keep in mind that this is also counting the ./ and ../ directories.
List the contents of your home directory by adding a tilde after the ls command.
List the contents of your root directory.
List the contents of the parent directory.
List the contents of all sub directories.
ls -d */
Only list the directories in the current directory.
List all files by the time they were last modified in reverse order. In other words the last files listed are the most recently modified files.