A-Z Popular Blog Encyclopedia Search »
Technology
 Advertisements
Related Guides
Linux
Information Technology

Integration

It Management

Systems Technology

Information Security

Technology Management

Networks

Technology Culture

Artificial Intelligence

Low Technology

Computing

Coding

23 Useful Ls Commands in Linux

 ,
The command ls, short for list, is a tool for listing the contents of directories in linux. This command accepts a large number of arguments and can work with other commands in a variety of ways. Most of these possibilities aren't very interesting and are seldom used. The ls commands below are powerful, useful or potentially useful in rare situations.

Powerful

Variations of the ls command that you may end up using over and over because they are applicable to common tasks.

ls -altr

List all files including hidden files, show a detailed view and sort by modification time with newest last. This is greedy because it shows all files and details. It is lazy because it shows the latest files at the bottom where the listing stops scrolling. This minimizes the chance you'll need to scroll up. If you only remember one variation of the ls command this is a good choice.

ls -alSr

Sort by file size with the biggest files last. This can be used to display the smallest files last by removing the -r at the end.

ls -alt | more

Show a detailed view with newest files first and show only one page of results at a time. This pipes the results of the ls to a command called more that breaks output into pages. The normal way to page down is to hit the space bar. This command gets annoying fast if you ls a directory with thousands of files that corresponds to hundreds of screens. This can normally be interrupted with control-c.

ls -1 | wc -l

Count the number of files and directories. The command ls -1 lists the contents of a directory in a single column. The command wc, short for word count, is a tool for counting newline, word, and byte count. The parameter -l asks for a line count which counts the number of files and directories as they have been formatted into a single column.

Obvious But Important

A few pointers that will be obvious to intermediate users of linux.

ls

There is nothing wrong with the default behavior of ls as it lists file and directory names neatly in multiple columns with no details. This has high information density.

ls /tmp /usr

You can pass ls a list of directories to list.

ls ../..

This syntax lists two directories up from your current working directory.

Potentially Useful

A list of common arguments for the ls command and other useful syntax.

-a

Show hidden files.

-l

Show a detailed listing that includes file permissions, owner, group, size, date and file name.

-lh

The -h argument is human readable. This does nothing on its own but works with any argument that displays file sizes to show sizes in gigabytes, megabytes and kilobytes as opposed to bytes that are often a long incomprehensible number.

-tr

Sort by modification time in ascending order. If you remove the -r, ls will sort in descending order. The -r stands for reverse.

-sh

Display allocated size in a human readable format. The argument -s is for size.

-S

Sort by file size. This is easy to confuse with the command above. Unfortunately, linux often includes case sensitive syntax that represents a type of latent human error. By default, -S sorts in descending order. This can be reversed with -Sr.

-I

This argument accepts a pattern and then ignores anything that matches this pattern in the listing. This is useful when you have a directory filled with one type of file but you want to see everything else. For example, ls -I *.jpg will ignore any .jpg files.

Filters

The ls command allows filters based on file or directory name. These filters can contain wildcards with the most commonly used wildcard being an asterisk that simply matches any text or empty space. The following are examples of filters for the ls command. It should be noted that the ls command accepts several wildcards that can perform complex filters. However, the simple filters below are far more commonly used.

ls me.jpg

Only list the file me.jpg

ls *.jpg

List any file that ends in .jpg

ls *me*

List any file that contains the text "me" in its name.

ls me*

List any file with a name that begins "me"

Rarely Useful

Arguments for ls that are rarely used either because they do something obscure or offer functionality that is better served by other commands.

-i

List the index number of each file. These are a unique id associated with each file that are useful in unusual circumstances. For example, if you have trouble deleting a file with a problematic file name, it is possible to delete the file by index number using the find command.

-R

Recursively list all subdirectories and their files. At first, this would appear to be a powerful feature. However, its functionality is weak in comparison to the command find and is therefore mostly unused.

-F

Displays a slash at the end of directory names to make it easier to tell the difference between files and directories. Many terminal emulators color code things so that directories appear in a different color from files. As such, this is rarely used.

-n

Display the user and group that own each file and directory as an id (UID and GID).

-q

Display non-printable file name characters as "?" This is only useful when you are trying to debug a problem with a strange character in a file name. This is a reasonably common type of problem in linux because there are virtually no restrictions on the types of characters that can be used in a filename but many of these characters do not print properly by default in terminal emulators. This means that when you cut and paste file names to a command, they often do not match the file's real name.

Notes

See the man pages for ls for rare and unusual ls arguments.
Overview: ls
Type
Definition
A tool for listing the contents of directories in linux.
Related Concepts

Linux Commands

This is the complete list of articles we have written about linux commands.
ls
shutdown
xargs
If you enjoyed this page, please consider bookmarking Simplicable.
 

Linux Commands

A list of unusually useful linux commands with brief explanations.

Linux Processes

The basic types of linux process.

Shutdown

A list of useful shutdown, halt, reboot and power-off commands for linux.

xargs

A list of xargs commands for linux you might actually use.

Application Server

An overview of application server with examples.

Transcoding

The definition of transcoding with examples.

Static Content

The definition of static content with examples.

Dynamic Content

The definition of dynamic content with examples.

Computer Science Examples

The definition of computer science with examples.

Command Prompt

The definition of command prompt with examples.
The most popular articles on Simplicable in the past day.

New Articles

Recent posts or updates on Simplicable.
Site Map