Command Linux LS: How to List Files and Directories in Linux | Learn Now!

Share On

In the world of Linux, the ls command is an essential tool for listing files and directories. Whether you’re a beginner or an experienced user, understanding how to use the ls command effectively is crucial for navigating and managing your files in the Linux operating system.

This comprehensive guide will walk you through the various options and parameters of the ls command, providing you with a solid foundation for using this powerful tool. From basic usage to advanced techniques, you’ll learn everything you need to know to become proficient in listing files and directories in Linux.

So, if you’re ready to take your Linux skills to the next level and master the ls command, keep reading!

Introduction

In this section, we’ll provide an overview of the ls command and its importance in the Linux ecosystem. We’ll explore its basic usage and explain why it’s a fundamental tool for managing files and directories.

What is the ls command in Linux?

The ls command is a command-line utility in Linux that allows users to list files and directories in a specified location. It provides a detailed view of the contents of a directory, including file names, permissions, ownership, size, and modification timestamps.

By default, the ls command lists the files and directories in the current working directory. However, you can also specify a different directory as an argument to list its contents.

Basic Usage of the ls command

In this section, we’ll cover the basic usage of the ls command. We’ll explore the different options and parameters that can be used to customize the output and provide more detailed information about the files and directories.

Listing Files and Directories

ls

The simplest form of the ls command is just “ls” without any options or arguments. This command lists the files and directories in the current working directory in a basic format.

For example:

$ ls
file1.txt  file2.txt  directory1  directory2

This command lists the files “file1.txt” and “file2.txt” and the directories “directory1” and “directory2” in the current working directory.

ls -l

The “-l” option is used to display the files and directories in a long format. This format provides more detailed information about each file and directory, including permissions, ownership, size, and modification timestamps.

For example:

$ ls -l
-rw-r--r-- 1 user group  1024 Jan  1 10:00 file1.txt
-rw-r--r-- 1 user group  2048 Jan  1 11:00 file2.txt
drwxr-xr-x 2 user group  4096 Jan  1 12:00 directory1
drwxr-xr-x 2 user group  4096 Jan  1 13:00 directory2

This command lists the files and directories in a long format, displaying the permissions, ownership, size, and modification timestamps for each entry.

ls -a

The “-a” option is used to display all files and directories, including hidden files and directories that start with a dot (“.”)

For example:

$ ls -a
.  ..  file1.txt  file2.txt  .hidden_directory

This command lists all files and directories in the current working directory, including hidden files and directories.

ls -al

The combination of the “-a” and “-l” options (“-al”) is commonly used to display all files and directories in a long format.

For example:

$ ls -al
total 16
drwxr-xr-x  4 user group  4096 Jan  1 10:00 .
drwxr-xr-x 10 user group  4096 Jan  1 09:00 ..
-rw-r--r--  1 user group  1024 Jan  1 10:00 file1.txt
-rw-r--r--  1 user group  2048 Jan  1 11:00 file2.txt
drwxr-xr-x  2 user group  4096 Jan  1 12:00 directory1
drwxr-xr-x  2 user group  4096 Jan  1 13:00 directory2

This command lists all files and directories in the current working directory in a long format, including hidden files and directories.

ls -lh

The “-lh” option is used to display the file sizes in a human-readable format, such as kilobytes (KB), megabytes (MB), or gigabytes (GB).

For example:

$ ls -lh
-rw-r--r-- 1 user group 1.0K Jan  1 10:00 file1.txt
-rw-r--r-- 1 user group 2.0K Jan  1 11:00 file2.txt
drwxr-xr-x 2 user group 4.0K Jan  1 12:00 directory1
drwxr-xr-x 2 user group 4.0K Jan  1 13:00 directory2

This command lists the files and directories in a long format, displaying the file sizes in a human-readable format.

ls -R

The “-R” option is used to list files and directories recursively. This means that it will list the contents of subdirectories as well.

For example:

$ ls -R
.:
file1.txt  file2.txt  directory1  directory2

./directory1:
file3.txt

./directory2:
file4.txt

This command lists the files and directories in the current working directory, as well as the contents of the subdirectories “directory1” and “directory2”.

ls -t

The “-t” option is used to sort the files and directories by modification time, with the most recently modified files or directories appearing first.

For example:

$ ls -t
file2.txt  file1.txt  directory2  directory1

This command lists the files and directories in the current working directory, sorted by modification time in descending order.

ls -S

The “-S” option is used to sort the files and directories by size, with the largest files or directories appearing first.

For example:

$ ls -S
directory2  directory1  file2.txt  file1.txt

This command lists the files and directories in the current working directory, sorted by size in descending order.

ls -r

The “-r” option is used to reverse the order of the listing, displaying the files and directories in reverse order.

For example:

$ ls -r
directory2  directory1  file2.txt  file1.txt

This command lists the files and directories in the current working directory in reverse order.

ls -i

The “-i” option is used to display the inode number of each file and directory.

For example:

$ ls -i
123456 file1.txt  234567 file2.txt  345678 directory1  456789 directory2

This command lists the files and directories in the current working directory, displaying the inode number for each entry.

ls -d

The “-d” option is used to list directories themselves, rather than their contents.

For example:

$ ls -d
directory1  directory2

This command lists the directories “directory1” and “directory2” in the current working directory, without listing their contents.

ls -F

The “-F” option is used to add a trailing character to each entry to indicate its type. For example, a “/” is added to directories, a “*” is added to executable files, and a “|” is added to symbolic links.

For example:

$ ls -F
file1.txt  file2.txt  directory1/  directory2/

This command lists the files and directories in the current working directory, with a trailing “/” added to directories.

Advanced Usage of the ls command

In this section, we’ll explore some advanced usage of the ls command. We’ll cover additional options and parameters that can be used to further customize the output and perform more specific listing operations.

ls –color

The “–color” option is used to enable colorized output, making it easier to distinguish between different types of files and directories.

For example:

$ ls --color
file1.txt  file2.txt  directory1  directory2

This command lists the files and directories in the current working directory with colorized output.

ls –help

The “–help” option is used to display a help message that provides information about the ls command and its available options.

For example:

$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.
  -a, --all                  do not ignore entries starting with .
  -A, --almost-all           do not list implied . and ..
      --author               with -l, print the author of each file
  -b, --escape               print C-style escapes for nongraphic characters
      --block-size=SIZE      scale sizes by SIZE before printing them; e.g.,
                               '--block-size=M' prints sizes in units of
                               1,048,576 bytes; see SIZE format below
  -B, --ignore-backups       do not list implied entries ending with ~
  -c                         with -lt: sort by, and show, ctime (time of last
                               modification of file status information);
                               with -l: show ctime and sort by name;
                               otherwise: sort by ctime, newest first
  -C                         list entries by columns
      --color[=WHEN]         colorize the output; WHEN can be 'always' (default
                               if omitted), 'auto', or 'never'; more info below
  -d, --directory            list directories themselves, not their contents
  -D, --dired                generate output designed for Emacs' dired mode
  -f                         do not sort, enable -aU, disable -ls --color
  -F, --classify             append indicator (one of */=>@|) to entries
      --file-type            likewise, except do not append '*'
      --format=WORD          across -x, commas -m, horizontal -x, long -l,
                               single-column -1, verbose -l, vertical -C
      --full-time            like -l --time-style=full-iso
  -g                         like -l, but do not list owner
      --group-directories-first
                             group directories before files;
                               can be augmented with a --sort option, but any
                               use of --sort=none (-U) disables grouping
  -G, --no-group             in a long listing, don't print group names
  -h, --human-readable       with -l and/or -s, print human readable sizes
                               (e.g., 1K, 234M, 2G)
      --si                   likewise, but use powers of 1000 not 1024
  -H, --dereference-command-line
                             follow symbolic links listed on the command line
      --dereference-command-line-symlink-to-dir
                             follow each command line symbolic link
                               that points to a directory
      --hide=PATTERN         do not list implied entries matching shell PATTERN
                               (overridden by -a or -A)
      --hyperlink[=WHEN]     hyperlink file names; WHEN can be 'always'
                               (default if omitted), 'auto', or 'never'
      --indicator-style=WORD  append indicator with style WORD to entry names:
                               none (default), slash (-p),
                               file-type (--file-type), classify (-F)
  -i, --inode                print the index number of each file
  -I, --ignore=PATTERN       do not list implied entries matching shell PATTERN
  -k, --kibibytes            default to 1024-byte blocks for disk usage;
                               used only with -s and per directory totals
  -l                         use a long listing format
  -L, --dereference          when showing file information for a symbolic
                               link, show information for the file the link
                               references rather than the link itself
  -m                         fill width with a comma separated list of entries
  -n, --numeric-uid-gid      like -l, but list numeric user and group IDs
  -N, --literal              print raw entry names (don't treat e.g. control
                               characters specially)
  -o                         like -l, but do not list group information
  -p, --indicator-style=slash
                             append / indicator to directories
  -q, --hide-control-chars   print ? instead of non graphic characters
      --show-control-chars   show non graphic characters as-is (default
                             unless program is 'ls' and output is a terminal)
  -Q, --quote-name           enclose entry names in double quotes
      --quoting-style=WORD   use quoting style WORD for entry names:
                               literal, locale, shell, shell-always,
                               shell-escape, shell-escape-always, c, escape
      --sort=WORD            sort by WORD instead of name: none (-U), size (-S),
                               time (-t), version (-v), extension (-X)
      --time=WORD            with -l, show time as WORD instead of default
                               modification time: atime or access or use (-u);
                               ctime or status (-c); also use specified time
                               as sort key if --sort=time (newest first)
      --time-style=TIME_STYLE  time/date format with -l; see TIME_STYLE below
  -t                         sort by modification time, newest first
  -T, --tabsize=COLS         assume tab stops at each COLS instead of 8
  -u                         with -lt: sort by, and show, access time;
                               with -l: show access time and sort by name;
                               otherwise: sort by access time, newest first
  -U                         do not sort; list entries in directory order
  -v                         natural sort of (version) numbers within text
  -w, --width=COLS           set output width to COLS.  0 means no limit
  -x                         list entries by lines instead of by columns
  -X                         sort alphabetically by entry extension
  -Z, --context              print any security context of each file
  -1                         list one file per line.  Avoid 'n' with -q or -b
      --help     display this help and exit
      --version  output version information and exit

SIZE may be (or may be an integer optionally followed by) one of following:
KB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.

The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).

The TIME_STYLE argument can be full-iso, long-iso, iso, locale, or +FORMAT.
FORMAT is interpreted like in date(1).  If FORMAT is FORMAT1FORMAT2,
then FORMAT1 applies to non-recent files and FORMAT2 to recent files.
TIME_STYLE prefixed with 'posix-' takes effect only outside the POSIX locale.
Also the TIME_STYLE environment variable sets the default style to use.

Using color to distinguish file types is disabled both by default and
with --color=never.  With --color=auto, ls emits color codes only when
standard output is connected to a terminal.  The LS_COLORS environment
variable can change the settings.  Use the dircolors command to set it.

Exit status:
 0  if OK,
 1  if minor problems (e.g., cannot access subdirectory),
 2  if serious trouble (e.g., cannot access command-line argument).

This command displays a help message that provides detailed information about the ls command and its available options.

ls –version

The “–version” option is used to display the version information of the ls command.

For example:

$ ls --version
ls (GNU coreutils) 8.30
Packaged by Homebrew

This command displays the version information of the ls command.

ls –ignore

The “–ignore” option is used to specify a pattern of files and directories to ignore when listing the contents of a directory.

For example:

$ ls --ignore=*.txt
directory1  directory2

This command lists the files and directories in the current working directory, ignoring any files with the “.txt” extension.

ls –ignore-backups

The “–ignore-backups” option is used to ignore backup files when listing the contents of a directory. Backup files are typically created by text editors and have a “~” character appended to their names.

For example:

$ ls --ignore-backups
file1.txt  file2.txt  directory1  directory2

This command lists the files and directories in the current working directory, ignoring any backup files with the “~” character appended to their names.

ls –ignore-case

The “–ignore-case” option is used to perform a case-insensitive listing of files and directories.

For example:

$ ls --ignore-case
file1.txt  file2.txt  directory1  directory2

This command lists the files and directories in the current working directory, performing a case-insensitive listing.

ls –ignore-glob

The “–ignore-glob” option is used to specify a glob pattern of files and directories to ignore when listing the contents of a directory.

For example:

$ ls --ignore-glob=*.txt
directory1  directory2

This command lists the files and directories in the current working directory, ignoring any files with the “.txt” extension.

ls –ignore-special

The “–ignore-special” option is used to ignore special files when listing the contents of a directory. Special files include device files, named pipes, and sockets.

For example:

$ ls --ignore-special
file1.txt  file2.txt  directory1  directory2

This command lists the files and directories in the current working directory, ignoring any special files.

ls –ignore-time

The “–ignore-time” option is used to ignore the timestamps of files and directories when listing their contents.

For example:

$ ls --ignore-time
file1.txt  file2.txt  directory1  directory2

This command lists the files and directories in the current working directory, ignoring their timestamps.

ls –ignore-vcs

The “–ignore-vcs” option is used to ignore version control system files and directories when listing the contents of a directory. This option is useful when you want to exclude version control files from the listing.

For example:

$ ls --ignore-vcs
file1.txt  file2.txt  directory1  directory2

This command lists the files and directories in the current working directory, ignoring any version control system files and directories.

ls –ignore-vcs-ignores

The “–ignore-vcs-ignores” option is used to ignore the ignore patterns specified by the version control system when listing the contents of a directory. This option is useful when you want to include ignored files in the listing.

For example:

$ ls --ignore-vcs-ignores
file1.txt  file2.txt  directory1  directory2

This command lists the files and directories in the current working directory, ignoring the ignore patterns specified by the version control system.

ls –ignore-vcs-files

The “–ignore-vcs-files” option is used to ignore version control system files when listing the contents of a directory. This option is useful when you want to exclude version control files from the listing.

For example:

$ ls --ignore-vcs-files
file1.txt  file2.txt  directory1  directory2

This command lists the files and directories in the current working directory, ignoring any version control system files.

ls –ignore-vcs-dirs

The “–ignore-vcs-dirs” option is used to ignore version control system directories when listing the contents of a directory. This option is useful when you want to exclude version control directories from the listing.

For example:

$ ls --ignore-vcs-dirs
file1.txt  file2.txt  directory1  directory2

This command lists the files and directories in the current working directory, ignoring any version control system directories.

ls –ignore-vcs-all

The “–ignore-vcs-all” option is used to ignore all version control system files and directories when listing the contents of a directory. This option is useful when you want to exclude all version control files and directories from the listing.

For example:

$ ls --ignore-vcs-all
file1.txt  file2.txt  directory1  directory2

This command lists the files and directories in the current working directory, ignoring all version control files and directories.

ls –ignore-vcs-untracked

The “–ignore-vcs-untracked” option is used to ignore untracked files and directories when listing the contents of a directory. This option is useful when you want to exclude untracked files and directories from the listing.

For example:

$ ls --ignore-vcs-untracked
file1.txt  file2.txt  directory1  directory2

This command lists the files and directories in the current working directory,

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *