What you should know about ls command in Linux?
Just going to start Linux basics? Then you should not miss “ls command” which have all ingredients of Linux basics. In order to understand ls command output you should know following stuff.
1) File types in Linux
2) Permissions and ownership’s of files
3) Special permissions like SUID, SGID, ACL’s, Sticky bit, SELinux.
4) Users and groups concepts
5) Inodes
6) Checking file modification times.
7) Directory structure
8) Relative and absolute paths
9) How file and folder sizes are calculated
10) Colouring scheme of types of files.
11) Linux wild cards
What not, every basic concept you should know is there in ls command output. The ls command was given front and back on internet which show different options available in it. But I feel some of them really miss explaining some basic concepts for Linux newbie. This post is meant for it.
There is one saying, if you know ls command in and out you are ready to explore Linux advanced concepts. OK, I am just kidding. But learning ls command will give you great insights in to Linux usage. This post is bit detailed and targeted at newbies of Linux.
Basic listing of files using ls command
Example 1: List contents of a directory.
ls
Output:
surendra@sanne-taggle:~/code/sh$ ls 99abc.txt fpga_build.sh startup_script.sh temp wifiactivate.sh
Example 2: This is not giving what type of files and folders are listed here, ls command provides a good feature from where you can distiging which what type of file is it.
ls -F
Output:
surendra@sanne-taggle:~/code/sh$ ls -F 99abc.txt fpga_build.sh* startup_script.sh temp/ wifiactivate.sh*
If you observe fpga_build.sh is ended with *, temp is ended with /. To decode these special characters you should use below table.
* Executable file
/ folder
@ Link file
| Pipe file
= Socket file.
Example 3: Most of the time ls command output is shown in color in many Linux distributions. This is because ls command is set as alias to coloring scheam.
surendra@sanne-taggle:/var$ alias | grep ls alias ls='ls --color'
Below is the table which shows what color corresponding to what file type.
explain each color for each time
Regular file white
Directory blue
Executable green
Socket file pink
Block file orange
Example 4: By default ls command will display files and folders in horizontal fashon, we can change by using -1(hyphen one).
surendra@sanne-taggle:~/code/sh$ ls -1 99abc.txt fpga_build.sh startup_script.sh temp wifiactivate.sh
Example 5: List inodes for each file we listed.
surendra@sanne-taggle:~/code/sh$ ls -i
791983 99abc.txt 789398 fpga_build.sh 805386 startup_script.sh 827714 temp 788671 wifiactivate.sh
Example 6: The above output is not looking good right? Try with -1(hifen one) as shown below.
surendra@sanne-taggle:~/code/sh$ ls -i1 791983 99abc.txt 789398 fpga_build.sh 805386 startup_script.sh 827714 temp 788671 wifiactivate.sh
The number on left hand side corresponds to inode and right hand side it’s “file name”.
Example 7: List all the files and folders including hidden files and folders in a given folder.
ls -a
. .. 99abc.txt fpga_build.sh startup_script.sh temp wifiactivate.sh
Long listing of files for more details.
Example 8: With above examples, we can not do much. We require more detailed output of ls command. For long listing, we can use ls -l.
Note: ls -l command output varies from one operating system to other. The below output is from Ubuntu machine.
surendra@sanne-taggle:~/code/sh$ ls -l
total 28
-rw-rw-r– 1 surendra surendra 0 Feb 10 19:32 99abc.txt
-rwxrwxr-x 1 surendra surendra 365 May 12 2015 fpga_build.sh
-rw-rw-r– 1 surendra surendra 663 Aug 24 20:37 startup_script.sh
drwxrwxr-x 2 surendra surendra 16384 Aug 23 18:30 temp
-rwxr-xr-x 1 root root 80 Aug 4 2015 wifiactivate.sh
This output require a detailed explanation and below are those details.
First line: “total 28” represent size of the given folder in bytes by default.
Note: Do not consider this as actual folder size, this is actual size of files + each folder block size(4k).
-rw-rw-r– 1 surendra surendra 0 Feb 10 19:32 99abc.txt
First 11 characters(field 1):The first filed can be divided in to sub fields as given below.
Note: You may see only 10 characters most of the time, even in this example too you can see only 10 characters.
a) First character: This character represents what is the type of the file you are looking at. Below are types of files and their corresponding single character representation.
Table with file types
b) Next 9 characters: These characters represents different type of permissions for three different chatagories.
Again these 9 characters can be dived in to 3 more sub categores as given below.
i) First three characters: This represents permissions given to owner of the file.
ii) Next three characters: These represents permissions given to group where the owner of the file belongs.
iii) All three characters: These represents permissions given to all other people who is not owner of this file or person who do not belong to the mention group.
c) The 11th character: This is a special character which is set when SELinux or ACL’s are enabled. Below is the table which shows what character represent what property.
Links(field 2): This field represents number of file links to the given file. If you see link as 2, that indicates we can access two more files/folders when you access this file.
Owner and group name (field 3 and 4): In this the first field represents the owner of the file and second represents group which have permissions on the file.
Size(field 5): This is the size of the file in bytes.
File modification(field 6, 7 and 8):These fields represents month, date and time of modification/creation of that file.
File name(field 9): This field represents name of the file.
Example 8: See the size in human readable form as kb’s, MB’s, GB’s, TB’s using -h option.
ls -lh
Output:
surendra@sanne-taggle:/var$ ls -lh total 56K drwxr-xr-x 2 root root 4.0K Feb 20 12:52 backups drwxr-xr-x 20 root root 4.0K Jan 12 10:41 cache drwxrwsrwt 2 root whoopsie 4.0K Oct 26 12:02 crash drwxr-xr-x 85 root root 4.0K Jan 12 11:31 lib drwxrwsr-x 2 root staff 4.0K Oct 16 2014 local lrwxrwxrwx 1 root root 9 Jan 29 2015 lock -> /run/lock drwxrwxr-x 20 root syslog 4.0K Feb 22 08:23 log drwxrwsr-x 2 root mail 4.0K Oct 23 2014 mail drwxrwsrwt 2 root whoopsie 4.0K Oct 23 2014 metrics drwxr-xr-x 2 root root 4.0K Oct 23 2014 opt lrwxrwxrwx 1 root root 4 Jan 29 2015 run -> /run drwxr-xr-x 8 root root 4.0K May 5 2015 spool drwxrwxrwt 48 root root 12K Feb 23 08:38 tmp drwxr-xr-x 3 root root 4.0K Feb 11 2015 www
Example 9: How about listing file with modified time order. By defaults top line file is latest modfied file and last line is modfied long time back.
surendra@sanne-taggle:/var$ ls -lt
total 56
drwxrwxrwt 48 root root 12288 Feb 23 08:44 tmp
drwxrwxr-x 20 root syslog 4096 Feb 22 08:23 log
drwxr-xr-x 2 root root 4096 Feb 20 12:52 backups
drwxr-xr-x 85 root root 4096 Jan 12 11:31 lib
drwxr-xr-x 20 root root 4096 Jan 12 10:41 cache
drwxrwsrwt 2 root whoopsie 4096 Oct 26 12:02 crash
drwxr-xr-x 8 root root 4096 May 5 2015 spool
drwxr-xr-x 3 root root 4096 Feb 11 2015 www
lrwxrwxrwx 1 root root 9 Jan 29 2015 lock -> /run/lock
lrwxrwxrwx 1 root root 4 Jan 29 2015 run -> /run
drwxrwsrwt 2 root whoopsie 4096 Oct 23 2014 metrics
drwxrwsr-x 2 root mail 4096 Oct 23 2014 mail
drwxr-xr-x 2 root root 4096 Oct 23 2014 opt
drwxrwsr-x 2 root staff 4096 Oct 16 2014 local
Example 10: Ok how about printing this in reverse order, ie lastest modified files at the end. We can use -r for this and combine with other options already learnt.
surendra@sanne-taggle:/var$ ls -ltr total 56 drwxrwsr-x 2 root staff 4096 Oct 16 2014 local drwxr-xr-x 2 root root 4096 Oct 23 2014 opt drwxrwsr-x 2 root mail 4096 Oct 23 2014 mail drwxrwsrwt 2 root whoopsie 4096 Oct 23 2014 metrics lrwxrwxrwx 1 root root 4 Jan 29 2015 run -> /run lrwxrwxrwx 1 root root 9 Jan 29 2015 lock -> /run/lock drwxr-xr-x 3 root root 4096 Feb 11 2015 www drwxr-xr-x 8 root root 4096 May 5 2015 spool drwxrwsrwt 2 root whoopsie 4096 Oct 26 12:02 crash drwxr-xr-x 20 root root 4096 Jan 12 10:41 cache drwxr-xr-x 85 root root 4096 Jan 12 11:31 lib drwxr-xr-x 2 root root 4096 Feb 20 12:52 backups drwxrwxr-x 20 root syslog 4096 Feb 22 08:23 log drwxrwxrwt 48 root root 12288 Feb 23 08:44 tmp
Example 11: Print files with larger size in at the end. This can be achieved by using -S option.
surendra@sanne-taggle:/var/log$ ls -lrhS -rw-r----- 1 syslog adm 144K Jan 20 09:36 kern.log.4.gz -rw-r----- 1 syslog adm 146K Feb 13 23:52 kern.log.2.gz -rw-r----- 1 syslog adm 190K Feb 23 08:29 kern.log -rw-r----- 1 syslog adm 223K Feb 18 14:44 syslog.4.gz -rw-rw-r-- 1 root utmp 287K Feb 11 18:42 lastlog -rw-r----- 1 syslog adm 288K Feb 22 08:23 syslog.1 -rw-r--r-- 1 root root 337K May 4 2015 udev -rw-r----- 1 syslog adm 364K Feb 21 08:55 auth.log.1 -rw-r----- 1 syslog adm 770K Feb 21 08:31 kern.log.1 -rw-r----- 1 syslog adm 1.4M Feb 23 08:45 syslog surendra@sanne-taggle:/var/log$
Advanced listing of files
Example 12: The ls command support many wild cards as well. The supported wildcards are as below.
* -Anything
[a-z] -Any single character between a to z
[0-9] -Any single digit between 0 to 9
[A-Z] -Any single character between A to Z
? -Any single character.
ls -l *.sh
Example 13 : Listing files with spaces is bit tricky. You should know more about escape character like \. With this character we can list file names with spaces.
surendra@sanne-taggle:~/code/sh$ ls -l total 28 -rw-rw-r-- 1 surendra surendra 0 Feb 10 19:32 99abc.txt -rw-rw-r-- 1 surendra surendra 0 Feb 23 08:57 abc test.txt -rwxrwxr-x 1 surendra surendra 365 May 12 2015 fpga_build.sh -rw-rw-r-- 1 surendra surendra 663 Aug 24 20:37 startup_script.sh drwxrwxr-x 2 surendra surendra 16384 Feb 13 18:21 temp -rwxr-xr-x 1 root root 80 Aug 4 2015 wifiactivate.sh surendra@sanne-taggle:~/code/sh$ ls -l abc\ test.txt -rw-rw-r-- 1 surendra surendra 0 Feb 23 08:57 abc test.txt
Example 14: How about listing files with special characters like * or & etc. We have to use same techniq to list them as well.
surendra@sanne-taggle:~/code/sh$ touch abc\&xyz.txt surendra@sanne-taggle:~/code/sh$ ls -l total 28 -rw-rw-r-- 1 surendra surendra 0 Feb 10 19:32 99abc.txt -rw-rw-r-- 1 surendra surendra 0 Feb 23 08:57 abc test.txt -rw-rw-r-- 1 surendra surendra 0 Feb 23 08:59 abc&xyz.txt -rwxrwxr-x 1 surendra surendra 365 May 12 2015 fpga_build.sh -rw-rw-r-- 1 surendra surendra 663 Aug 24 20:37 startup_script.sh drwxrwxr-x 2 surendra surendra 16384 Feb 13 18:21 temp -rwxr-xr-x 1 root root 80 Aug 4 2015 wifiactivate.sh
surendra@sanne-taggle:~/code/sh$ ls -l abc\&xyz.txt -rw-rw-r-- 1 surendra surendra 0 Feb 23 08:59 abc&xyz.txt
Example 15: The ls is such an adective command many people do not know an alternative for this command. Below are some alternatives to ls command.
surendra@sanne-taggle:~/code/sh$ echo * 99abc.txt fpga_build.sh startup_script.sh temp wifiactivate.sh surendra@sanne-taggle:~/code/sh$ find -maxdepth 1 . ./wifiactivate.sh ./99abc.txt ./startup_script.sh ./fpga_build.sh ./temp
With Compgen command
surendra@sanne-taggle:~/code/sh$ compgen -f wifiactivate.sh 99abc.txt startup_script.sh fpga_build.sh temp
Example 16: Keep shortcuts to frequently used ls commands
alias ll="ls -l" alias lt="ls -lrt" alias lS="ls -lrS" alias lp="ls -lF" alias l1="ls -1"
Feel something is missing? Just comment about it, we will update the post.
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018