😎
CS1.dev
  • Welcome to Computer Science 1
  • Unit 2
    • Activity 2.1
    • Activity 2.2
    • Activity 2.3
    • Activity 2.4
    • Activity 2.5
    • Activity 2.6
    • Activity 2.7
    • Activity 2.9
    • Activity 2.10
    • Project 2
    • Activity 2.8
    • Activity 2.11
    • Activity 2.12
    • Activity 2.13
    • Activity 2.14
    • Activity 2.15
    • Activity 2.16
    • Activity 2.17
    • Activity 2.18
    • Activity 2.19
    • Project 3
    • Activity 2.20
    • Activity 2.21
    • Activity 2.22
    • Activity 2.23
    • Activity 2.24
    • Project 4
    • Activity 2.25
    • Activity 2.26
    • Activity 2.27
    • Activity 2.28
    • Project 5
    • Activity 2.29
    • Activity 2.30
    • Activity 2.31
    • Activity 2.32
    • Activity 2.33
    • Activity 2.34
    • Activity 2.35
    • Activity 2.36
  • Unit 3
    • Activity 3.1
    • Activity 3.2
    • Activity 3.3
    • Activity 3.4
    • Activity 3.5
    • Activity 3.6
    • Activity 3.7
    • Activity 3.8
    • Activity 3.9
    • Activity 3.10
    • Activity 3.11
    • Project 6
    • Activity 3.12
  • Activity 3.13
  • Activity 3.14
  • Activity 3.15
  • Activity 3.16
  • Project 7
  • Activity 3.17
  • Activity 3.18
  • Activity 3.19
  • Project 8
  • Linux
    • bash
    • cat
    • cd
    • chmod
    • df
    • echo
    • find
    • grep
    • less
    • ls
    • mkdir
    • more
    • pwd
    • tar
    • touch
    • unzip
    • zip
Powered by GitBook
On this page
  1. Linux

ls

Table of Contents

  1. Introduction to ls

  2. Basic Usage of ls

  3. Common Options for ls

  4. Sorting Files with ls

  5. Displaying File Types and Inodes

  6. Showing Hidden Files and Directories

  7. Recursive Listing with ls

  8. Human-Readable Formats with ls

  9. Examples for Advanced Scenarios

  10. Conclusion and Further Reading

1. Introduction to ls


  • What is ls?: The ls command in Linux stands for "list files and directories". It's one of the most frequently used commands, allowing users to view the contents of a directory.

  • Why Use ls?: Understanding how to effectively use ls enhances your navigation and management capabilities within the Linux file system.

2. Basic Usage of ls


Command Syntax:

ls [OPTIONS] [FILE|DIRECTORY]

Basic Examples:

  • List Current Directory Contents:

    ls
  • List Specific Directory Contents:

    ls /path/to/directory
  • List a File with Details (Implicitly uses -l option for single files):

    ls filename

3. Common Options for ls


Option
Description
Example

-a

Show all files, including hidden ones

ls -a

-l

Use a long list format

ls -l

-d

List directory entries instead of contents

ls -d /path/to/dir

-h

With -l, print sizes in human readable format

ls -lh

-r

Reverse order while sorting

ls -lr

-t

Sort by modification time, newest first

ls -lt

4. Sorting Files with ls


  • Sorting Options:

    • -S: Sort by file size

    • -t: Sort by modification time (default newest first)

    • -r: Reverse the sort order

    • -u: Sort by access time

    • -X or --sort=extension: Sort alphabetically by extension

Examples:

  • Sort by Size in Descending Order:

    ls -Sl | less
  • Sort by Modification Time (Oldest First):

    ls -ltr

5. Displaying File Types and Inodes


  • Option for File Type Indication:

    • -F or --classify: Append indicator (one of */=>@|) to entries

Example:

  • Display with File Type Indicators:

    ls -lF

6. Showing Hidden Files and Directories


  • Option for Displaying Hidden Entries:

    • -a or --all: Do not ignore entries starting with .

Example:

  • List All Files Including Hidden Ones:

    ls -a

7. Recursive Listing with ls


  • Option for Recursive Listing:

    • -R or --recursive: List subdirectories recursively

Example:

  • Recursive List of Current Directory:

    ls -R
  • Detailed Recursive List:

    ls -lR

8. Human-Readable Formats with ls


  • Option for Human Readable Sizes:

    • -h when combined with -l: Prints sizes in human readable format (e.g., 1K, 234M, 2G)

Example:

  • Detailed List with Human Readable File Sizes:

    ls -lh

9. Examples for Advanced Scenarios


  • Find Executable Files in Current Directory and Subdirectories:

    ls -lR | grep "\-..x"
  • List Only Directories in the Current Path (including hidden ones):

    ls -ad */

10. Conclusion and Further Reading


  • Conclusion: Mastering ls enhances your Linux command-line experience, enabling efficient navigation and inspection of file systems.

  • Further Reading:

    • Manual Page: Access detailed documentation with man ls

    • GNU Coreutils Documentation: For comprehensive guides on ls and other essential utilities

    • Online Tutorials and Forums: Websites like LinuxConfig, Unix & Linux Stack Exchange for community-driven knowledge.

PreviouslessNextmkdir

Last updated 8 months ago