ls Command in Linux

Learn via video courses
Topics Covered

Overview

The 'ls' command in Linux is a fundamental command used by Linux users every day. The command provides a list of files and directories within the current directory. With the 'ls' command in Linux, users can explore the file system's hierarchical structure, gain insights about file permissions, and sort files based on various parameters.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax of ls Command in Linux

The basic syntax of the ls command in Linux is as follows:

Where:

  • options: These are the flags or switches that alter how the 'ls' command behaves. Options can be used to modify the output, for example, to show hidden files or to display detailed information about each file.
  • file|dir: This is an optional argument specifying the file or directory whose contents you want to list. If no file or directory is specified, 'ls' will list the contents of the current directory.

Options in ls Command in Linux

  1. -l: The '-l' option displays the output in a long listing format, showing detailed information about each file and directory.

    For example -

    Output:

    This output shows the file permissions, number of links, owner, group, size, and time of last modification for 'file1.txt'.

  2. -a: The '-a' option shows all files, including hidden files (those whose names start with '.').

    For example -

    Output:

    This output lists all files in the current directory, including hidden ones.

  3. -R: The '-R' option recursively lists subdirectories.

    For example -

    Output:

    This output shows the files in '/home/user' and its subdirectory 'dir1'.

Example Usages

  • Listing files in the current directory:

    Output:

    Explanation: This command lists all non-hidden files in the current directory.

  • Listing files in a specific directory:

    Output:

    Explanation: This command lists all non-hidden files in the directory '/home/user'.

Tips

  • You can combine options. For example, 'ls -la' will display detailed information about all files, including hidden ones.

  • The 'ls' command is case sensitive. Be sure to use the correct casing when specifying files or directories.

Advanced Use Cases of ls Command in Linux

  • Sorting files by modification time:

    Output:

    Explanation: This command lists files in long format sorted by modification time, with the most recently modified file first.

  • Displaying file sizes in human-readable format:

    Output:

    Explanation: This command lists files in long format with sizes displayed in a format that is easier for humans to understand (e.g., K for kilobytes, M for megabytes).

  • Displaying directories as if they were files:

    Output:

    Explanation: This command treats directories as if they were files, displaying their details rather than their contents.

Conclusion

  • The 'ls' command in Linux is a versatile tool for exploring the file system.

  • Options can greatly enhance the 'ls' command, providing detailed information about files and directories, displaying hidden files, and more.

  • By learning the 'ls' command and its options, you can efficiently navigate the Linux file system.

Hiring Partners:
GGoogleAAmazonMicrosoftFFlipkartAAdobe1200+ more