Methods of payment Abuse

What the ls command does in Linux

23.08.2024, 03:07

The ls command in Linux is used to display the contents of the current working directory in the terminal. By default, the ls command displays the names of files and subdirectories in the current directory.

What is the command for?

Команда ls в Linux

The ls command in Linux is used to display a list of files and directories in a specified directory. It is one of the most basic and frequently used commands in Unix-like operating systems. Here are the basic functions and parameters of the ls command:

Basic Functions

File and directory list: by default, the ls command displays the names of all files and folders in the current directory.

Useful parameters

Displays a detailed list including permissions, owner, file size, and modification date.

  1. Shows all files, including hidden files (those starting with a dot).
  2. Used in conjunction with -l to display file sizes in an easy-to-read format (e.g. KB, MB).
  3. Recursively displays the contents of all subdirectories.
  4. Sorts files by last modified time.
  5. Sorts files by size.

Usage examples

  1. ls: Displays a list of files and folders in the current directory.
  2. ls -l: Shows a detailed list of files and folders.
  3. ls -a: Shows all files, including hidden files.
  4. ls -lh: Shows a detailed list with convenient file sizes.

The ls command is useful for navigating the file system and getting information about the contents of directories.

Command options

Some common options for the ls command include:

-l: displays a long output format that includes additional information about each file, such as permissions, owner, group, size, creation date, etc.

-a: displays all files, including hidden files starting with a dot.

-h: displays file size in human-readable format (e.g., "1K", "2M", etc.).

-t: displays files in order of most recent modification, starting with the most recent.

-r: displays files in reverse order.

An example of using the ls command with the -l option:

$ ls -l
total 0
drwxr-xr-x 2 user user  6 Feb 28 14:25 dir1
-rw-r--r-- 1 user user  0 Feb 28 14:25 file1.txt
-rw-r--r-- 1 user user 12 Feb 28 14:25 file2.txt

In the above example, each line represents information about a file or directory in the current directory, including permissions, number of hard links, owner, group, size, date last modified, and file name.