Linux Commands – ls
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In this blog series, we will explore all the commands available in Linux. Today we will discuss one of the most basic commands in Linux to list the content of directories.
In computing, ls is a command to list computer files in Unix and Unix-like operating systems. ls is specified by POSIX and the Single UNIX Specification. When invoked without any arguments, ls lists the files in the current working directory. In other environments, such as DOS, OS/2, and Microsoft Windows, similar functionality is provided by the dir command.
An ls utility appeared in the original version of AT&T UNIX, the name inherited from a similar command in Multics also named ‘ls’, short for the word “list”.Today, the two popular versions of ls are the one provided with the GNU coreutils package, and that released by various BSD variants. Both are free software and open source, and have only minor syntax differences. The version of ls bundled in GNU coreutils was written by Richard Stallman and David MacKenzie.
1 2 3 4 |
## NAME: ls - list directory contents ## SYNOPSIS: ls [OPTION]... [FILE]... ## DESCRIPTION: List information about the FILEs (the current directory by default). ## AUTHOR: Written by Richard M. Stallman and David MacKenzie. |
Linux Commands – ls:
How to list contents of the current directory in Linux?
1 2 |
## List contents of the current directory ls |
How to list contents of a specific directory in Linux?
1 2 3 |
## List contents of a specific directory ## ls ls /var |
How to get details of file\directory in Linux?
1 2 3 4 5 6 7 8 9 10 11 12 |
## List using a long listing format ls -l ## drwxrwxr-x 2 user1 group1 6 Mar 30 15:45 dir1 ## -rw-rw-r-- 1 user2 group2 2 Mar 30 15:15 file1 ## lrwxrwxrwx 1 root root 7 Jun 30 2019 bin -> usr/bin ## FileType UserPermission GroupPermission WorldPermission NumberOfLinks Owner Group SizeInBytes LastModified FileName ## -------- -------------- --------------- --------------- ------------- ----- ----- ----------- ------------ ---------------------------- ## d rwx rwx r-x 2 user1 group1 6 Mar 30 15:45 dir1 ## - rw- rw- r-- 1 user2 group2 2 Mar 30 15:15 file1 ## l rwx rwx rwx 1 root root 7 Jun 30 2019 bin -> usr/bin |
Ls Long Listing Output Explained:
FileType: The 1st Character represent the file type. In Linux you have different types of files, below are some of the file types available in Linux and their representation in the ls output.
- d = directory
- – = regular file
- l = symlink
- c = character device
- p = named pipe
- s = socket
- b = block device
FilePermission: The next nine characters represent the file permissions and are divided into a chunk of three characters (representing read, write and execute permissions) with the first three representing user permission, second three representing group permission and last three for all user (world) permission. Below are the permissions available for files in Linux with their representation in the ls output.
- r = Read
- w = write
- x = execute
- – = no access
- s = setgid bit
- t = sticky bit
NumberOfLinks: This number is the hardlink count of the file, when referring to a file, or the number of contained directory entries, when referring to a directory.
Owner: The owner of the file.
Group: The group of the file.
SizeInBytes: The size of the file in bytes.
LastModified: The date and time when the file was last modified.
FileName: The name of the file.
How to list hidden files and directories in Linux?
1 2 |
## List hidden files and directories ls -la |
How to list only directories in Linux?
1 2 3 4 5 |
## List only directories in Linux ls -d */ ## List only directories under a specific directory /var/ ls -d /var/*/ |
How to list directories recursively in Linux?
1 2 3 4 5 6 7 8 9 10 11 |
## List current directory recursively ls -R ## List specific directory recursively ls -R ## List full path recursively one per line ls -R /:$/&&f{s=$0;f=0} /:$/&&!f{sub(/:$/,"");s=$0;f=1;next} NF&&f{ print s"/"$0 }' |
How to list file and directory size in Linux?
1 2 3 4 5 6 7 8 |
## List file and directory size ls -lh ls -lh /var ls -al --block-size=K #List file size in KB ls -al --block-size=M #List file size in MB ls -al --block-size=G #List file size in GB sudo du -sh /var #List total size of a directory sudo du -sh /var/* | sort -hr #short by file size |
How to list file and directory last access time in Linux?
1 2 3 |
## List file and directory access time and modification time ls -l #by default prints modification time' ls -lu #prints last access time |
How to list file and directory sorted by time, name, extension etc.
1 2 3 4 5 6 7 |
## Get a sorted list of file and directory ls -l #sort file name alphabetically by default ls -lr #reverse the sorted alphabetically output ls -lX #sort alphabetically by extension ls -lS #sort by size ls -lt #sort by modification time ls -lv #sort by version |
How to list files with specific name, extension in Linux?
1 2 3 4 |
## List file with specific pattern ls -la *.txt #List files with specific extension ls -l /dev/sda* #List files starting with sda ls -l /dev/?ty* #List files having ty in name as 2nd and 3rd charecter from begining |
Hope you have enjoyed this blog post. In the next blog post, we will discuss cd command in linux.