December 21, 2024

Basic ‘ls’ Command Examples in Linux

Basic ls command with all its parameters:

ls -a list all files including hidden file starting with ‘.’

ls –color colored list [=always/never/auto]

ls -d list directories – with ‘ */’

ls -F add one char of */=>@| to enteries

ls -i list file’s inode index number

ls -l list with long format – show permissions

ls -la list long format including hidden files

ls -lh list long format with readable file size

ls -ls list with long format with file size

ls -r list in reverse order

ls -R list recursively directory tree

ls -s list file size

ls -S sort by file size

ls -t sort by time & date

ls -X sort by extension name

ls command with most used options

ls shows files and directories in present working directory. (if no arguments are passed.) (It doesn’t show hidden files which starts with . by default.)

 user@ubuntu14:/usr$ ls
 bin games include lib lib32 local sbin share src

To see all files (hidden files/folders also). Use ls -a OR ls -all

 user@ubuntu14:/usr$ ls -a
 . .. bin games include lib lib32 local sbin share src

To differentiate between files and folders and symbolic links and other, use ls -F OR ls –classify

 user@ubuntu14:~$ ls -F
 bash_profile_course chat_apps/ Desktop/ Downloads/ foxitsoftware/
 Public/ test/ bin/ ClionProjects/ Documents/ IDE/ Music/
 Pictures/ Templates/ Videos/

Here, ending characters are used to distinguish files and folders.

 “/” suggest directory.
 “*”suggest executables.
 “@” suggest symbolic links.

To get more details about the files and directories, use ls -l

user@ubuntu14:~/example$ ls -l
 total 6464

 -rw-r--r-- 1 dave dave 41 Dec 24 12:19 Z.txt
 drwxr-xr-x 2 user group 4096 Dec 24 12:00 a_directory
 -rw-r--r-- 1 user group 6 Dec 24 12:01 a_file
 lrwxrwxrwx 1 user group 6 Dec 24 12:04 a_link -> a_file
 -rw-r--r-- 1 user group 6 Dec 24 12:03 a_newer_file
 -rw-r----- 1 user group 6586816 Dec 24 12:07 big.zip

In this example, the total size of the contents is 6460KB.

Then there is an entry for each file/directory in alphabetical order with upper case before lower case.

The first character is the type (e.g. d – directory, l – link).

The next 9 characters show the permissions for the user, group and other.

This is followed by the number of hard links, then the owner’s name and group.

The next field is the size in bytes. This can be displayed in a human-friendly form by adding the -h option e.g. 6586816 is displayed as 6.3M

There then follows a timestamp (usually the modification time).

The final field is the name. Note: links also show the target of the link.

Vedant Kumar

Currently I'm working as an Implementation Engineer, Started my career as an System Administrator - Linux. Additionally loves to explore new technologies and research about new open-source software that ease the development cycle.

View all posts by Vedant Kumar →

Leave a Reply

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


The reCAPTCHA verification period has expired. Please reload the page.

close

Ad Blocker Detected!

VEDANT EXPLAINS
We've noticed that you are using an ad blocker. Advertising helps fund our server cost and keep it truly independent. It helps to build our content creator team. So please disable your ad blocker, and help us to keep providing you with free- great content - for free. Thank you for your support.

Refresh