Ls command is used to list the files and subdirectories under a specific directory. It is one among the regularly used commands while doing administration of a server.
Above given are a couple of options which are most regularly used with ls command.
We will see some ls practical examples below for better understanding of it.
Listing the files and directories:
By typing normal ls command on the terminal window, will list all of the files and subdirectories under a directory.
Sample Usage case,
Assume that we have a software package unzipped on Linux/Unix flavor machine. Now, to see what are the files and sub-directories are available within that we can use this command.
We can take a numerous number of examples for “ls command”, above is just a sample case. Normal “ls command” output is,
The long listing of files and directories:
From the normal “ls command”, we could see the only list of things available but it won’t show up more details about it. Such as,
Whether it is a file or directory?
Who are the owner & group ownership of that file or directory?
What are the files permissions level and last modified dates? etc
We can see all of the above information from using “ls -l” command option.
A sample command output is given below,
From “ls -l” command output we can make out,
Starting from left,
The first character explains whether it is a file or directory.
Next, 9 characters explain what permission levels are for the user, primary group and others in sequence.
Next character talks about file inode information.
Who are the primary owner and group owner of the files is explained?
File size and next time/date of file modified information.
Files with reverse order:
To display the files in reverse chronological order. We can use “-r” option with ls command.
One can observe that based on the file alphabetical order we can see the files organized. Sample output is given below.
Files and directories with the timestamp and reverse timestamp:
For displaying the files and directories based on timestamp, we can use “-t” option with ls command.
Sample command output is given below,
We can combine -r option with -t timestamp, for files and directories in reverse timestamp.
This is most used in day to day life because it gives the recent modified files and directories information in the top order. Keep note of this for sure.
Sample ls -ltr command output is,
Files and directories with human readable size format:
As we have discussed, ls -l command will display the files and directories size information also but they are in blocks format. We need to convert them into kilo bytes and Mega bytes etc to understand clearly.
Instead, we can use “-h” option with ls command to display the sizes in human readable format.
sample command output is given below,
Let’s remove the -l option and observe the output,
Ls -h command output here….
Similar to above we can try,
ls -s To display file sizes in bytes
ls -k To display file sizes in kilo bytes
ls -m To display file sizes in mega bytes
By using ls –s command we can do this.
Listing the hidden files and directories:
Regular ls command will not display any hidden files and sub directories under a directory. Let’s first understand,
what is a hidden file or directory? And Why we need that in the first place?
In a day to day activities, we might want to hide critical/important files and directories from the regular user. This is just to avoid any accidental changes to those files etc.
for example, some application specific configuration files.
To hide a file or directory, we need to use “.” Before file/directory name.
Example, .profile, .bash_profile etc
To view these hidden files and directories, we can use “-a” option with ls command.
Sample command output is given below,
Listing out inode numbers with files and directories:
If you are new to inode numbers, then visit this, what are inode numbers? How are they associated with files and directories structure?
A usage case,
When do we check inode numbers for files?
Practical command output is given below,
By using ls –I command we can do this