ls command is most basic command of Linux. This guide provides you with some basic use of Linux ls command. This command lists files in specified directory. Once you are into terminal mode, it is the first command you need to be familiar with.
Use of ls command Linux
Basic Use of ls Command in Linux
ls when used alone simply displays list of files , directories and links in current working directory.
Syntax
ls
ls command can also be used to view files in directory other than working directory. You can use ls command followed by slash with directory name.
Syntax
ls /directory_name
Exapmple
ls /etc
Note: If you dont have permission to read file or directory, you will be returned with error.
View detailed file information using ls command
ls -l uses a long listing format showing
- type of file file type – for normal file , d for directory, s for socket file and l for link file
- permission – is given in 9 character first three for user, second three for group, last three for others
- owner – provides owner user name
- group – provides owner group name
- size
- creation date , time
- file name.
As you can see in above image where we have used both basic and ls -l command.
if you use ls -l on directory it will list details about directory content rather than directory itself. To view properties of directory itself use ls -ld
View details for working directory using ls -ld
ls -ld will list details of working directory itself rather than its contents.
Syntax
ls -ld
Note: using ls -ld option you will see details of working directory rather than for content of directory.
View size of files and directories in KB, MB, GB using ls with option – lh
h is human readable format. The files size is mentioned in KB, MB and GB. Using this option will save your time consumed in understanding.
Syntax
ls -lh (Shows size of files / folder in working directory)
ls -lh /directory name (Shows size of files / folder in directory name mentioned)
Example
ls -lh /var
View hidden files using ls command with option –a
There are several files like system administration related files in Linux which are hidden, these have “. (period)” in beginning of their name. -a option will show hidden files. In list generated all files starting with .(Dot) are hidden.
To hidden files in Linux use -a switch with command
Syntax
ls -a
View last modified time using ls command with option -lt
-lt option will display order of their modified time. Order will be last modified file on top.
Syntax
ls -lt
View last modified time using ls command with option -ltr
-ltr option will display list of files in reverse modified time order i.e. file modified last will be at bottom of list.
Syntax
ls -ltr
List files and directories recursively with ls command using option -R
-R option will show files recursively showing content of directory. Entire directory tree structure will be visible for current working directory, You can specify directory also is you wan to view tree structure of directory other than working directory.
Syntax
ls -R (Shows tree structure of working directory)
ls -R /directory name (Shows tree structure of directory name mentioned)
Other articles which may interest you