📜  ls in linux - Shell-Bash (1)

📅  最后修改于: 2023-12-03 14:44:04.508000             🧑  作者: Mango

ls in Linux - Shell/Bash

Introduction

In Shell/Bash programming, ls is a command-line utility used to list the files and directories in a given directory. It is used to view file and folder information, such as file permissions, ownership, size, and timestamps.

Syntax

The basic syntax of the ls command is as follows:

ls [OPTIONS] [FILE]...
  • OPTIONS: Specifies various options to modify the output format or behavior of the ls command.
  • FILE: Specifies the file(s) or directory(ies) to list. If no file or directory is provided, the current working directory is assumed.
Common Options

Here are some commonly used options with the ls command:

  • -l: Long listing format. Displays detailed information about files and directories, including permissions, ownership, size, and timestamps.
  • -a: List all files and directories, including hidden ones.
  • -h: Human-readable sizes. Prints file sizes in a more readable format, such as "1K", "100M", etc.
  • -S: Sort files by size instead of the default alphabetical order.
  • -t: Sort files by modification time, with the newest first.
  • -r: Reverse the order of the sort.
Examples

Let's take a look at some examples to understand the usage of the ls command:

  1. List all files and directories in the current directory:
ls
  1. List all files and directories in a specific directory:
ls /path/to/directory
  1. List all files and directories in long listing format:
ls -l
  1. List all files and directories, including hidden ones:
ls -a
  1. Sort files by size:
ls -S
  1. Sort files by modification time in reverse order:
ls -t -r
Conclusion

The ls command is a powerful tool in Linux/Unix systems for listing files and directories. Understanding its various options allows programmers to efficiently navigate and work with the file system from the command line.