List Files And Directories Using 'ls' Command

Introduction

Before moving further let us look at the previous article, Basic UNIX Commands Everyone Should Know

ls command is an important command in Unix where every UNIX user and system admin cannot live without this two letter command. Whether you use it 10 times a day or 1000 times a day, knowing the power of ls command can make your journey of usin the ls command line enjoyable.

We can list out the names of the files available in the directory with this two letter word 'ls'.

ls command will list all the files available in the directory when no filename as an option is passed in the command else it will display the list of the files matching with the filename.

Syntax

The syntax for the ls command is:

ls [options] [names]

In this article we will learn some of the practical examples of 'ls' command:

List Files using ls with no option

ls with no option as an argument list files and directories in simple format where we won't be able to view details of the files like its file types, size, modified date and time, permission etc.

 

List root directory (ls /)

By issuing the 'ls /' command with the root directory as an argument, the content of the root directory can be viewed, assuming the user has proper permissions.

List all subdirectories (ls *)

This command will list the content of all subdirectories.

Listing file attribute (-l)

This is the -l (long) option which is one of the most frequently used command which will tell your the size of the file along with its access rights permissions, last modification time and ownership details.

 

View hidden files (-a)

Hidden files begin with a dot (.). There are certain hidden files in every directory that normally don't show up in the listing. The -a option (all) lists all the hidden files along with the other files.

 

List files in Human readable format (ls -lh)

h stand for human readable format which will display file size in easy to understand format i.e M for MB, K for KB and G for GB. Due to this the size of file is in an easy to understand format.

 

Identifying directories and executables (ls -F)

This command will mark all the executable files with * and list the directories with '/' character at the end.

 

List files in reverse order (ls -r)

You can reverse the order of the files with the -r (reverse) option with the names sorted in reverse ASCII collating sequence.

 

Recursively listing (ls -R)

The -R (recursive) option lists all directories and sub-directories in a directory tree. The traversal of the directory tree is done recursively till there are no sub-directories left.

 

Display One File Per Line Using ls -1

This command will show single entry per line.

 

List directory information (ls -ld)

When we use 'ls -l' we will get the details of directories content but when we need the details of a directory we can use -ld option as shown below.

 

Sort by date/time (ls -t)

This command will list the files on the basis of modification time.

Firstly I am running the command to display the last modified file and after creating a new file I am again running the same command to display the newly created file in the starting of the modified files list.

 

File output in Multiple columns (ls -x)

When we have multiple files, it is better to display the filenames in multiple columns. The name of the files will start from left to right in ascending order.

 

File Listing by modification in long listing format (ls -lt)

In the above command the -l will show the modification time of a file and -t option presents the files in the order of their modification time, the last modified file is placed first. It is a type of long listing format sorted by date/time.

 

Sort files by file size (ls -S)

This command sorts files/directories list by file size by using ls -S command. We can also sort by file size in a long listing format using ls -lS command.

 

Display UID and GID of files (ls -n)

Unix system uses UID and GID numbers to map usernames and groupnames. UID is a positive integer assigned to each user, where GID is a positive integer used to represent a specific group. We can easily check our UID and GID with the 'id' command followed by your username.

With the ls -n command the UID and GID is displayed in numeric format instead of name.

In the above picture I am showing the id of username 'abhishekarora' and displaying the long listing format of files with and without username.

 

File Listing by access time (ls -lut)

A file also has an access time i.e the last time someone read, wrote or executed the file. Access time is distinctly different from modification time that gets set only when the contents of the files are changed. The -u option displays the access time. The files are listed in order of their access time when -u option is coupled with -t option.

 

ls command and it's Aliases

We can make alias for ls command as user defined. When we execute ls command it will take the user defined option as an argument by default. For ex if we execute the alias ls="ls -l" command the ls command will take -l option by default.

When we execute the ls command without any option it prints out the list of files and directories in a color format. and after making an alias for the ls statement, the ls command prints out the long listing format of files as shown below.

 

Show version of ls command

To check for the version of ls command we can use 'ls --version' command.

 

Show help page command

'ls --help' command will list the help page of 'ls' command with their options.

 

Summary

Below are the commands we have learned in this article:

CommandDescription
ls /The content of the root directory can be viewed
ls *To list the content of all subdirectories
ls -lLong listing showing seven attributes of a file
ls -aShowing the hidden files which begins with a dot(.)
ls -lhShowing the files size of an file in human readable format
ls -FMarks executables with * and directories with /
ls -rSorts files in reverse order
ls -RRecursive listing of all files in sub directories
ls -1Display single entry per line
ls -ldTo list the directory information
ls -tTo list files on basis of modification time
ls -xTo display files in multiple columns
ls -ltTo display files in long listing format sorted by date/time
ls -STo sorts files by file size
ls -nTo display UID and GID in numeric format instead of name
ls -lutList files in order of their access time
ls --versionTo check the version of ls command
ls --helpTo list the help page of ls command

With the help of this article we learned some of the practical examples of 'ls' command.

I hope you liked this article. Thanks for reading. Your valuable comments and queries are most welcomed. 


Similar Articles