Linux “ls” Command with Examples – Atatus

On Linux, the “ls” command is one of the most commonly used. Used to display a list of files and subdirectories in the current directory. If you’re new to using the command line, the first command you should learn is probably ls. This command can be used by both normal users and system administrators.

The ability to see what files are in a directory is what makes ls so important. It will be used frequently to display the contents of the directory. Although it is not a complicated command, it comes with a number of options to list files with additional information. Although ls is always enough to list content, you are likely to find some of these options very useful.

Linux “ls” command options

A Linux command has the following basic syntax

:ls [ Options ] [File]

The following are some frequently used options in Linux ls commands:

Options Description ls -a list of all files, including the hidden file that begins with ‘.’. ls -d list directories – with ‘ */’. List ls -l with long format – show permissions. ls -F Add the flag (one of */=>@|) to the entries. ls -lh This command will show you the file sizes in human-readable format. List ls -r in reverse order. Inode number (index) of the ls -ilist file. ls -ltr See reverse output order by date. ls -t sort by time and date. ls -n Used to print the group ID and owner ID instead of their names. ls -m A comma-separated list of entries should fill the width. ls -g This allows you to exclude the owner and group information columns. ls -q Force printing of non-graphic characters in file names such as the ‘?’; character. ls -Q Place double quotation marks around the names of the entries.

Basic

examples of the Linux ls command

Here, we will look at the basics of ls command examples in a Linux environment with all available options.

  1. The ‘ls’ command is used to enumerate
  2. files and directories Show hidden files and directories Show

  3. complete information about
  4. files

  5. Classify files with special characters
  6. Show file

  7. index number
  8. View last edited file
  9. Display

  10. file size in human-readable format
  11. Show reverse output order by date List all files and directories in

  12. reverse
  13. order

  14. UID and GID list
  15. of files and directories List files and directories separated by a comma

  16. List all files and directories
  17. without owner details Show

  18. subdirectories without other
  19. files Display version of ls command Show

  20. The

  21. help
  22. page for the ls command The ‘ls’ command is used

  23. to enumerate files and
  24. directories

The contents of your current working directory, which is just a technical way of indicating the directory your terminal is currently in, will be listed if you run the “ls” command without further options. ls

linux command ls 5
Linux Command

ls Show hidden files and directories

Use the -a option of the ls command to display hidden files and directories in the current directory.

ls -a

Files that begin with the period are hidden (.). The current directory (.) as well as the parent directory (..) are displayed by “ls -a”.

linux command ls 5
Linux ls

-a command

Show complete information about files

The “ls -l” option displays the contents of the current directory in a long list format, one per line. The line starts with file or directory permission, owner and group name, file size, date and time of creation/modification, file/folder name as some of the attributes.

ls -l

linux command ls a
Linux ls

-l command Classify files with

special characters

The ls command categorizes files using the -F parameter. Means, Directories ending with a forward slash (/), Files that can be executed with a trailing asterisk (*), Symbolic links with a final speed symbol (@), FIFO with a vertical bar final (|) and Regular files that contain nothing.

ls -F

linux command ls a

Show

file index number

For internal purposes, you may need to know the index number of a file. To display the index number, use the “ls -i” option. You can remove files with special characters in their names by using the index number.

ls -i

linux command ls l 2
Linux Command ls -i

View the last edited

file

The most recently modified file is displayed first when the file is sorted by modification time. Use the ls and head commands together to access the most recently edited file in the current directory.

ls -t ls -t | head -1

linux command ls l 2
Linux Command

ls -t

Display file size in human-readable format

Another frequent ls option is -h or –human-readable and -h should be used with -l and –s to print sizes like 1K 234M 2G, etc. This will display file sizes in human-readable format instead of bytes.

ls -lh When you use ls -lh, it displays all the information about the file or directory name and

when you use ls -sh it displays only the size and name of the file or directory name.

Using ls -lh:linux command ls f 1Linux ls –

lh command Using

ls -sh:

linux command ls f 1
Linux
ls –

sh

command

Show reverse output order by date

In the preceding command, the l argument is used for the long list format, the t argument sorts all files and directories according to the modification time and lists the most recent first, and the r argument is used to reverse the sort order.

ls -ltr As a result, the ls -ltr command lists all directories and file names at length by sorting the modified date in reverse order.

linux command ls i 1
Linux ls -ltr command

List all files and directories in reverse order The “ls -r” option lists

all files and directories in reverse order

.

Files and directories are sorted alphabetically in reverse.

ls -r

linux command ls i 1
Linux

ls -r command UID and GID list of

files and directories

The “ls -n” command displays the UID (user ID) and GID (group ID) of each file and directory, one per line. A typical user and group (UID and GID) have 1000, but the root UID and GID have zero.

ls -n

linux command ls t 1
Linux ls

-n command List files and directories separated by a comma

The “ls -m” command displays all files and directories separated by a comma.

ls -m

linux command ls t 1
Linux

ls -m command

List all files and directories

without owner details The “ls -g” option is similar to the “ls -l” option, however, the ‘-g

option ignores the file owner and directory details. ls –g

<img

src=”https://www.atatus.com/blog/content/images/2022/06/linux-command-ls-lh-1.png” alt=”” />Linux ls -g command

Show subdirectories without

other files This “ls -d */

” command can be used to show only subdirectories and hide all other files. ls -d *

/

linux command ls lh 1

Linux Command ls -d */”

Display version of

ls command Using this “ls -version” command

, you can check the version of the ls command.

ls -version

linux command ls sh 1
Linux Command

ls -version Show ls command Help

page

When using this “ls -help”, You can get a guide to the ls command. You have more options. Some of them are given below for reference.

ls -help

linux command ls sh 1
Linux Command ls -help

WindUp

In this article, some of the options for the ls command are listed above with the examples. It is one of the simplest commands in Linux. Even if you are familiar with these commands, you may not be familiar with all the specified circumstances.

Monitor your entire

application with Atatus

Atatus is a full-stack observability platform that allows you to review issues as if they occurred in your application. Instead of guessing why errors occur or asking users for screenshots and log dumps, Atatus lets you replay the session to quickly understand what went wrong.

We offer application performance monitoring, real user monitoring, server monitoring, log monitoring, synthetic monitoring,

uptime monitoring, and API analytics. It works perfectly with any application, regardless of the framework, and has plugins.

linux command ls ltr 1

Atatus can be beneficial to your business as it provides a complete view of your application, including how it works, where performance bottlenecks exist, which users are most affected, and what bugs break your code for your frontend, backend, and infrastructure.

If you’re not already an Atatus customer, you can sign up for a 14-day free trial.

Contact US