internet security software tech

How to Use the ls Command to List Files and Directories on Linux

A terminal window on a Linux laptop.Fatmawati Achmad Zaenuri/Shutterstock

We use the Linux ls command on a daily basis with out fascinated about it. That’s a pity. Pay it some consideration, and also you’ll in finding many helpful choices—together with some you must upload in your command-line arsenal.

ls Lists Files and Directories

The ls command is more than likely the primary command maximum Linux customers stumble upon. Those people who cling across the command line use it day in and time out with out even fascinated about it. That may give an explanation for why there’s extra to this command than maximum customers notice. We checklist information with it to peer what’s in a listing. We checklist information in lengthy layout once we need to take a look at the permissions on a document. Beyond that, it will get little attention.

The ls command is one of the ones instructions with a wealth of choices. Perhaps this is a part of the issue. There are such a lot of choices, how do you sift via them to search out the helpful ones? And having discovered them, how do you take into account them?

Useful diversifications of the ls command with their strings of choices and parameters are the easiest applicants for aliases. In truth, in maximum distributions, what you bring to mind because the “bare” ls command is in truth an alias. Amongst different issues, the kind command can be utilized to display the underlying definition of aliases. Let’s take a look at the definition of ls:

kind ls

type ls in a terminal window

The --color=auto parameters are integrated mechanically each and every time you employ the ls command. This is what supplies the other colours for the other document sorts within the listings.

RELATED: How to Create Aliases and Shell Functions on Linux

Simple ls Listings

Everyone who’s spent a while the use of the Linux terminal is aware of that, by means of default, ls lists the information and directories within the present listing.


ls in a terminal window

If you need to have your checklist produced ina unmarried column, use the -1 (one document according to line) choice:

ls -1

ls -1 in a terminal window

We’ll speak about that weird-looking filename on the most sensible of the checklist in a minute.

Using ls on Different Directories

To have ls checklist the information in a listing rather then the present listing, move the trail to the listing to ls at the command line. You too can move greater than one listing to ls, and feature them indexed one after the opposite. Here, we’re asking ls to checklist the information in two directories, one known as “Help” and the opposite known as “gc_help.”

ls Help gc_help

ls Help gc_help in a terminal window

When ls has indexed the contents of the primary listing it lists the contents of the second one. It prints the identify of each and every listing because it processes them:

Name of the directory being displayed by ls before the contents are listed.

Using File Patterns

To selectively checklist a suite of information, use trend matching. The query mark “?” will constitute any unmarried personality and the asterisk “*” will constitute any string of characters. To checklist any information or directories that experience names beginning with “ip_” use this layout:

ls ip_*

ls ip_* in a terminal window

To checklist information that experience “.c” extensions, use this layout:

ls *.c

ls *.c in a terminal window

You too can use ls with grep , and use grep‘s trend matching functions. Let’s search for any information that experience the string “_pin_” of their identify:

ls | grep _pin_

ls | grep _pin_ in a terminal window

This is nearly the similar as the use of ls by itself, with two wildcards:

ls | grep _pin_
ls *_pin_*

ls | grep _pin_ in a terminal window

Why nearly the similar? Note the other layouts. grep forces the output to a unmarried filename according to line layout.

Non-Printing Characters

It is conceivable to search out your self with a filename that has a non-printing or control-character in its filename. Typically it will occur while you amplify an archive you’ve downloaded from the internet or retrieved a git repository, and the unique writer made a mistake making a document however didn’t spot it.

Our bizarre document is one of those:

Filename with control character in it in a terminal window

If we take a look at it within the document browser and press “F2” to rename it, the non-printing characters are represented by means of a bizarre image.

Filename with a control characater in it, in the renaming dialog window

You can use the -b (get away) strategy to will let you see what the document identify in truth incorporates. This choice reasons ls to make use of the get away sequences of the C programming language to constitute the control-characters.

ls -b a*

ls -b a* in a terminal window

The mysterious personality is printed to be a newline personality, represented in C as “n.”

Ignoring Files

To have positive information ignored from a list, use the --hide choice. Suppose you don’t need to see the backup “.bak” information within the checklist. You may use this command:

ls --hide=*.bak

ls --hide=*.bak in a terminal window

The “.bak” information aren’t integrated in the second one checklist.

The Long Format Listing

The -l (lengthy checklist) choice reasons ls to supply detailed details about each and every document.

ls -l

ls -l in a terminal window

There’s a large number of knowledge right here, so let’s step via it.

The very first thing ls presentations is the whole dimension of the entire information within the checklist. Then each and every document or listing is displayed on a line on its own.

The first set of ten letters and dashes are the document kind and the landlord, crew and different document permissions.

The first actual personality represents the document kind. It might be one of:

  • :  An ordinary document.
  • b: A block particular document.
  • c: A personality particular document.
  • d: A listing.
  • l: A symbolic hyperlink.
  • n: A community document.
  • p: A named pipe.
  • s: A socket.

The subsequent nine characters are three teams of three characters displayed contiguously. Each crew of three constitute the learn, write, and execute permissions, in that order. If the permission is granted, there might be an r, w, or x provide. If the permission isn’t granted, a hyphen - is proven.

The first set of three characters are the permissions for the document proprietor. The 2d set of three permissions are for crew contributors, and the closing set of three permissions is for others.

Sometimes the execution permission for the proprietor is represented by means of an s. This is the setuid bit. If it’s provide, it implies that the document is accomplished with the privileges of the document proprietor, now not the consumer executing the document.

The execution permission for the crew can be an s.  This is the setgid bit. When that is implemented to a document, it approach the document might be accomplished with the privileges of the ower’s crew. When used with a listing, any information created within it’ll take their crew permissions from the listing they’re being created in, now not from the consumer who’s growing the document.

The execution permission for the others can on occasion be represented by means of a t. This is the sticky bit. It is generally implemented to directories. If that is set, without reference to the write and executable privileges which might be set at the information within the listing, handiest the document proprietor, the listing proprietor, or the foundation consumer can rename or delete information within the listing.

A not unusual use for the sticky bit is on folders equivalent to “/tmp”. This is writable by means of all customers at the laptop. The sticky bit at the listing guarantees that customers—and processes introduced by means of the customers—can handiest rename or delete their very own brief information.

We can see the sticky bit at the “/tmp” listing. Note the usage of the -d (listing) choice. This reasons ls to record on the main points of the listing. Without this selection, ls will record at the information within the listing.

ls -l -d /tmp

ls -l -d /tmp in a terminal window

RELATED: How to Use the chmod Command on Linux

The quantity following the permissions is the selection of onerous hyperlinks to the document or listing. For a document, that is generally one, but when different onerous hyperlinks are created, this quantity will build up. A listing generally has no less than two onerous hyperlinks. One is a hyperlink to itself, and the opposite is its access in its mum or dad listing.

The identify of the landlord and crew are displayed subsequent. They are adopted by means of the document dimension and the date of the closing amendment of the document. Finally, the filename is given.

Human Readable File Sizes

Having the document sizes in bytes isn’t at all times handy. To see the document sizes in probably the most suitable devices (Kilobytes, Megabytes, and many others.) use the -h (human-readable) choice:

ls -l -h

ls -l -h in a terminal window

Showing Hidden Files

To see hidden information, use the -a (all) choice:

ls -l -a

ls -l -a in a terminal window

The two entries “.” and “..” constitute the present listing and the mum or dad listing, respectively. A document known as “.base_settings” is now visual for the primary time.

Omitting . and .. from Listings

If you don’t need your checklist cluttered up with the “.” and “..” entries, however you do need to see hidden information, use the -A (nearly all) choice:

ls -l -A

ls -l -A in a terminal window

The hidden document remains to be indexed, however the “.” and “..” entries are suppressed.

Listing Directories Recursively

To have ls checklist the information in all subdirectories use the -R (recursive) choice

ls -l -R

ls -l -R in a terminal window

ls works its approach via all the listing tree under the beginning listing, and lists the information in each and every subdirectory.

output from ls recursively listing directories

Displaying the UID and GID

To have the consumer ID and crew ID displayed as a substitute of the consumer identify and crew identify, use the -n (numeric uid and gid) choice.

ls -n

ls -n in a terminal window

Sorting The Listings

You can type the checklist by means of extension, document dimension, or amendment time. These choices don’t need to be used with the lengthy checklist layout, however it generally is smart to take action. If you’re sorting by means of document dimension, it is smart to peer the document sizes within the checklist. When you’re sorting by means of extension kind, the lengthy checklist layout isn’t so vital.

To type by means of extension, use the -X (type by means of extension) choice.

ls -X -1

ls -X -1 in a terminal window

The directories are indexed first (no extensions in any respect) then the remainder apply in alphabetical order, in line with the extensions.

To type by means of document dimension, use the -S (type by means of document dimension) choice.

ls -l -h -S

ls -l -h -S in a terminal window

The type order is biggest to smallest.

To type the checklist by means of amendment time, use the -t (type by means of amendment time) choice.

ls -l -t

ls -l -t in a terminal window

The checklist is taken care of by means of the amendment time.

If the document amendment time is throughout the present 12 months, the ideas displayed is the month, day, and time. If the amendment date was once now not within the present 12 months, the ideas this is displayed is the month, day, and the 12 months.

A snappy option to get the latest and oldest information in a listing is to make use of ls with the head and tail instructions.

To get the latest document or listing, use this command:

ls -t | head -1

To get the oldest document or listing, use this command:

ls -t | tail -1

ls -t | head -1 in a terminal window

To Reverse the Sort Order

To opposite any of the type orders, use the -r (opposite) choice.

ls -l -h -S -r

ls -l -h -S -r in a terminal window

The checklist is now ordered from the smallest document to the biggest document.

And there’s extra

Check out the person web page for lsthere are lots of extra choices. Some of them fulfill slightly difficult to understand use circumstances, however every now and then, you’ll be satisfied you realize about them.

Do you wish to have to peer the document timestamps with the utmost precision that Linux may give? Use the full-time choice:

ls --full-time

Perhaps you need to peer the inode selection of the information? Use the inode choice:

ls -i

Are you running on a monochrome show and need to take away all chance of complicated information for directories and hyperlinks? Use the classify choice, and ls will append one of those to each and every checklist access:

  • /: A listing.
  • @: A symlink.
  • |: A named pipe.
  • =: A socket.
  • *: An executable information
ls -F

Do some digging. You’ll in finding that ls is a wealthy vein, and also you’ll stay turning up gem stones.