howtouselinux

Understanding ls command in Linux

Table of Contents

 

The ls command is one of the most basic commands in Linux. It prints the contents of a directory to the screen. By default, it prints the names of all of the files and directories in the directory, but you can use various flags to display more information.

ls command in Linux: list the contents of the directory

  • You can use the ls command to list the contents of a directory. For example, if you want to list the contents of your home directory, you would type: ls ~
  • You can use the ls command to list the contents of multiple directories. For example, if you want to list the contents of your home directory and the /tmp directory, you would type: ls ~ /tmp

 

ls command -l option: show more information about each file

You can use the -l flag with the ls command to show more information about each file, such as the permissions, owner, group, size, and modification time. For example, if you want to list all of the files in the current directory with more information, you would type: ls -l

ls command -R option: recursively list the contents of all subdirectories

You can use the -R flag with the ls command to recursively list the contents of all subdirectories. For example, if you want to list all of the files in the current directory and all subdirectories recursively, you would type: ls -R

ls command -a option: show all files including hidden files.

You can use the -a flag with the ls command to show all files, including hidden files. Hidden files are typically denoted by a . (dot) at the beginning of the filename. For example, if you want to list all of the files in the current directory, including hidden files, you would type: ls -a

ls command -t option: sort the output by modification time

You can use the -t flag with the ls command to sort the output by modification time. For example, if you want to list all of the files in the current directory, sorted by modification time, you would type: ls -lt

ls command -s option: sort the output by file size

You can use the -S flag with the ls command to sort the output by file size. For example, if you want to list all of the files in the current directory, sorted by file size, you would type: ls -lS

You can use multiple flags with the ls command. For example, if you want to list all of the files in the current directory, including hidden files, sorted by modification time, you would type: ls -lat

ls command -h option: print file sizes in human-readable format

You can use the -h flag with the ls command to print file sizes in human-readable format. For example, if you want to list all of the files in the current directory, sorted by file size in ls command, with file sizes in human-readable format, you would type: ls -lhS

ls command -r option: reverse the order of the output

You can use the -r flag with the ls command to reverse the order of the output. For example, if you want to list all of the files in the current directory, sorted by modification time in ls command, in reverse order, you would type: ls -ltr

ls command options

  • You can use the –color flag with the ls command to colorize the output. For example, if you want to list all of the files in the current directory, with colors indicating the file type, you would type: ls –color
  • You can use the -i flag with the ls command to show the inode number of each file. The inode number is a unique identifier for each file. For example, if you want to list all of the files in the current directory, with their inode numbers, you would type: ls -li
  •  You can use the -F flag with the ls command to show a trailing / for directories, a trailing * for executable files, and a trailing @ for symbolic links. For example, if you want to list all of the files in the current directory, with indicators for each file type, you would type: ls -F
  • You can use the –time-style flag with the ls command to specify how the modification time is displayed. For example, if you want to list all of the files in the current directory, with the modification time displayed in ISO 8601 format, you would type: ls –time-style=iso
  • You can use the -d flag with the ls command to list only the directories, not the files. For example, if you want to list all of the directories in the current directory, you would type: ls -d */
  • You can use the –group-directories-first flag with the ls command to list directories before files. For example, if you want to list all of the files and directories in the current directory, with the directories listed first, you would type: ls –group-directories-first

 

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Liquid Web Managed Word Press