Skip to content

Latest commit

 

History

History
154 lines (114 loc) · 3.42 KB

File metadata and controls

154 lines (114 loc) · 3.42 KB

ls Command in Linux

The ls command is used to list all files and directories present in a given directory.

To get manual documentation for any command, use:

man ls

This provides complete information about the ls command.


Options of ls Command

1) Basic Listing

ls

  • Displays all files and directories in alphabetical order.

ls -r

  • Displays all files and directories in reverse alphabetical order.

2) Paginated Output

ls | more

  • Displays content line by line.
  • Press q to exit.

ls | pg

  • Displays content page by page (each page contains 20 lines).
  • Press q to exit.

3) Detailed File Information

ls -l

  • Displays a long listing of files with details such as permissions, owner, size, and modification date.

ls -t

  • Displays files sorted by last modified date and time (most recent at the top).

ls -rt

  • Displays files sorted by last modified date in reverse order (oldest at the top).

ls -i

  • Displays inode numbers for each file.
  • Inodes store file attributes such as size, owner, creation date, last modified time, etc.

4) Hidden Files

ls -a

  • Displays all files, including hidden files (. and .. are included).

ls -A

  • Displays almost all files, excluding . and ...

5) File Type Identification

ls -F

  • Displays files with type indicators:
    • Directory/
    • Executable file*
    • Link file@

Example Output:

initctl@   # Link File
pts/       # Directory
ls*        # Executable File

ls -f

  • Disables color formatting in the output.

6) Recursive and Block Usage

ls -R

  • Displays all files and directories, including subdirectory contents.

ls -s

  • Displays the number of blocks used by each file (1 block = 1KB in Ubuntu).

ls -h

  • Displays file sizes in human-readable format (e.g., KB, MB, GB).

Using ls with Other Commands

  • Paginated Display:

    ls /dev | less
    ls /dev | more
  • Show only a fixed number of files:

    ls /dev | head -5  # Displays the top 5 files
    ls /dev | tail -5  # Displays the bottom 5 files

Combining ls Options

Options can be combined in any order. The following commands are equivalent:

ls -l -t -r
ls -t -r -l
ls -l -r -t
ls -ltr
ls -trl

Common Questions

Q1) Command to display all files (including hidden files) in last modification order (oldest first), including inode numbers and block usage, in long listing format?

ls -atrisl

Example Output:

131279 4 -rw-r--r-- 1 user user 807 Jan 3 12:57 .profile
131277 4 -rw-r--r-- 1 user user 3771 Jan 3 12:57 .bashrc
162011 4 -rw-r--r-- 1 user user 220 Jan 3 12:57 .bash_logout

Q2) Which command lists all files, including hidden files, along with their inode numbers?

ls -ai

Q3) Command to make a long listing of all files, including hidden ones, sorted by modification date (oldest first)?

ls -latr

Q4) Does ls -r list files sorted by modification date (oldest first)?

  • False
  • ls -r lists files in reverse alphabetical order.
  • To list files by modification date (oldest first), use:
    ls -rt

Q5) Does ls -la produce the same result as ls -al?

  • True
  • The order of options does not matter: ls -la is equivalent to ls -al.