Listing files and folders in Linux File System
As part of this article, you will see how to list files and folders in Linux File System using the ls command.
👨🏽💻🧑🏻💻For more ARTICLES, FOLLOW📍DevOps Engineering on Cloud
Overview of ls command to list files and folders in Linux
The ls
command is one of the many Linux commands allowing users to list files or directories from the CLI.
Just like you navigate in your File Explorer or Finder with a GUI, the ls
command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.
When you type ls
you can see the files or folders present in the present working directory in ascending order as shown in the below picture👇🏻
You can get help from the terminal using the ls --help
command. You can check the Usage in the below picture👇🏻
🚨👉🏼 You can also check the complete udemy course (Linux Shell Commands for Absolute Beginners using Ubuntu 20x)🔗Referral link
Setup Files and Folders from GitHub Account to learn Shell Commands
You need more folders to practice the Linux commands so we have the repo with sample data. You can download it to your computer by following the steps below👇🏻
Make sure git is installed on your computer and then you can use the following command to clone the repo to your current working directory.👇🏻
Once the data folder is cloned to your computer, Then you can enter into the data folder using the cd
command. This is what the cloned folder looks like👇🏻
How to Setup Jupyter Lab? (to go through help on Linux commands)
If you want to learn how to set up the jupyter lab to practice the Linux command on the virtual environment in the most interactive way then you need to check out the following timestamp from the video👇🏻
Getting the properties of files and folders using ls command in Linux
When you type ls -ltr
you can see the output as follows. By seeing the color code you can differentiate between files and folders. The blue color is a folder and the white color is a file. 👇🏻
But, the other way to differentiate them is by looking at the first characters of the output. If it has d(directory) at starting then it's a folder.
Now let's know what ls -ltr
means. If you type ls --help
then you can read the usage of -ltr
👇🏻
-l use a long listing format
-t sort by time, newest first; see --time
-r, --reverse reverse order while sorting
Sorting ls output while listing files in Linux
You can sort the output by using the above ls -ltr
command.
📌 A clear explanation of what happened in the above code snippet will be in this video timestamp👇🏻
You can also use ls -l -t -r
the results will be the same as ls -ltr
List files and folders in Linux sorted by size
You can sort the files and folders based on the size -S
control argument. You can use the following set of control arguments to sort the files ls -S
you are using the uppercase alphabet S.
-S sort by file size, largest first
Without using
ls -S
With using
ls -S
You can see the output got sorted in descending order by file size in the 4th column
Overview of Hidden Folders and Files
To get the list of hidden files, use the following control argument along with ls
command
-a, --all do not ignore entries starting with .
So, the final commands will look like ls -a
To deep dive into the command, you can visit this timestamp video 👇🏻
Listing the files and folders recursively in Linux
To recursively visit the files and folders inside the particular directory you can use the following control argument. It's uppercase R.
-R, --recursive list subdirectories recursively
And the command is ls -R
You can tweak the above command by introducing
-l
to itls -lR
the output looks like the following👇🏻
Exploring Control Arguments while listing files in Linux
Control arguments are very powerful and helpful for being productive in day-to-day tasks in Linux. You can get complete help about how to use these control arguments from the terminal itself. Here is the summary of how to explore and use the control arguments in the Linux terminal without googling and wasting time.
📌 Timestamp of the video to explore control arguments in Linux terminal👇🏻
Overview of representation of file sizes
In Linux, you can use human-readable -h
control argument along with ls command like this👉🏼ls -lh
It will help to understand the file sizes by converting the bytes to a human-readable format.
-h, --human-readable with -l and -s, print sizes like 1K 234M
For example, you can check the below images.👇🏻
📌 Without using
-h
a control argument. Check out 4th column below
📌 With using
-h
a control argument in lsls -lh
. Check out 4th column below
The file sizes are in Megabytes and kilobytes like 1K 234M.
Overview of alias command and ll to list files and folders
In Linux, you can define an alias for the most frequently used commands. You can define it by using the following syntax👇🏻
alias ll='ls -alF'
📌If you want to learn about alias clearly with a video explanation check out the below video timestamp👇🏻
🙏🏼Thank you, for reading the article. If you find it valuable please follow our publication DevOps Engineering on Cloud
🚨👉🏼 You can also check the complete udemy course (Linux Shell Commands for Absolute Beginners using Ubuntu 20x)🔗Referral link