Site icon TechGit

15 Super Useful ls Command In Linux With Examples

This post explains how to use ls command in Linux with examples. You can use ls-r command in Linux or ls – l command in Linux or so others after reading this post.

ls Command In Linux

The ls command lists the contents of the current directory, or any other directory or file that is specified as an command option. Ls command can also be used to display the file permissions, ownership and modification time of the files in a directory.

Syntax of ls command in Linux

ls [option] [file/directory]

‘ls’ will display the contents of the current directory. It must be noted that by default, ‘ls’ lists files and directories in alphabetical order.

The ls command has a number of options or arguments which can be used. For example, the -a option can be used to list all files in a directory, including hidden files. The -l option can be used to list the contents of a directory in long format, which includes information about the file permissions, owner, group, size, and modification time.

The most common options used with ls command are:

  1. -l: known as a long format that displays detailed information about files and directories.
  2. -a: Represent all files Include hidden files and directories in the listing.
  3. -t: Sort files and directories by their last modification time, displaying the most recently modified ones first.
  4. -r: known as reverse order which is used to reverse the default order of listing.
  5. -S: Sort files and directories by their sizes, listing the largest ones first.
  6. -R: List files and directories recursively, including subdirectories.
  7. -i: known as inode which displays the index number (inode) of each file and directory.
  8. -g: known as group which displays the group ownership of files and directories instead of the owner.
  9. -h: Print file sizes in human-readable format (e.g., 1K, 234M, 2G).
  10. -d: List directories themselves, rather than their contents.

ls Command In Linux With Examples

Here are some examples of how to use ls command:

  1. Run the following command to list directory contents:

$ ls
  1. Run the following command to display One File Per Line (ls -1):
$ ls -1

$ ls -1

  1. Run the following command to display total information about Files/Directories(ls -l):
$ ls -l 
  1. Run the following command to show all the hidden files(ls -a OR ls -A):
$ ls -a

To show all the hidden files in the directory, use ‘-a option’. Hidden files in Unix starts with ‘.’ in its file name. Here “.” means current directory and “..” means parent directory are shown.So if you don’t want them to display use “-A” option. The ls -A can be used to see the hidden files, but not the ‘.’ (current directory) and ‘..’ (parent directory).

  1. Run the following command to list specific file types or extensions:
$ ls *.jpg

The above command is used to display all files with a .jpg extension.

  1. Run the following command to display files Recursively (ls -R):
$ ls -R SAN/
  1. Run the following command to display file size in human readable form(ls -lh file/directory name):
$ ls -lh 

NOTE: The size is displayed in the readable format. K stands for KB , M for MB , G for GB.

  1. Run the following command to order files based on last modified time(ls -lt):
$ ls -lt 
  1. Run the following command to order files based on last modified time in decending order(ls -lrt):
$ ls -lrt

To sort the file names in the last modification time in reverse order. This will show the last edited file in the last line.

  1. Run the following command to list directories only(ls -d */):
$ ls -d */
  1. Run the following command to List all subdirectories(ls *):
$ ls *
  1. Run the following command to sort files with Size(ls -S):
$ ls -lS
  1. Run the following command to redirect ls output to a file (ls > out.txt):
$ ls > out.txt
$ cat out.txt 
  1. Run the following command to open last edited file (ls -t):
$ vi first.txt
$ vi second.txt
$ vi `ls -t | head -1`
  1. You can also run ls command with multiple options:
$ ls -alF

It must be noted that while the ls command can also be used to list the contents of a file, but this is not its primary function.

Original Article

FacebookTwitterTumblrRedditLinkedInHacker NewsDiggBufferGoogle ClassroomThreadsXINGShare
Exit mobile version