Hey, guys welcome back on Linux command line for beginners and in this session, we will learn about ls command.
ls is a Linux shell command that lists directory contents of files and directories. Now, first of all, let’s open our terminal I have told you in the last Session. Just press ctrl + Alt + T to open the Terminal now for the ls command you need to know some syntax.
So we all know that ls command can be given by just writing ls on the terminal. But it has some Options so these options will come in as a second argument.
$ls [option ] [file or directory ]
So ls command comes with options first and then files and directory. Now I will show you what I mean by that.
SIMPLE ls COMMAND
So first of all simple ls command will you know work like this only which lists the content of a directory. So we know whenever we open the terminal we are in the home directory right so we can just say ls and it looks list us the contents of our home directory. Which is the desktop or other document directory or you know downloads or other directories.
Preview a Specific Directory
$ls [Directory Name]
Now The second option is using ls command is by using a directory name. For example, we want to see the contents of this usr directory so our usr directory contains these files. So how we can list the content of this usr directory you can just write ls and then just write usr and then press ENTER. It will list all the files which are there in the usr directory.
Right in the same way if you want to list out all the contents in any folder then I will just say ls “Directory name” and press ENTER. It will show us the contents of the Folder.
Preview home Directory
If you want to list out all the contents of our root folder just need to give ls / and press ENTER. It will show us the content of our root directory.
Now in other terms when you want to list out the content of home directory you can just say ls ~ which will also give you the same content as ls / will give you.
Show Previous Directory list
You can just write ls .. and this dot means you want to print out one step back directory structure. So whenever you press LS like this it will give the previous Directory. So whenever we want to print the content of home folder then we just need to give these two .. It’ll tell you just one folder back.
Preview Two-step Back Directory
If you want to go two folders back and you want to list out the content of two folders back then you can do ls ../.. and then press ENTER and you can see it will give you your directory.
List all files in Detailed Format
Now there is some flags or options you can use with ls so, For example, you give ls -l and this means you want to list out all the files and directories in long format and when you press ENTER. It will give you the listings in long format, which means it will give you the details so who is your user, What are the rights for this file, what is the size of your file, at which date this file was created and the time that which this file is created.
Show Hidden Files
Now there is one more command which is ls -a It will give you all the hidden files. So hidden files in Linux start from dot, okay and they will not be shown GUI you can see these dot files are not here so whenever you give ls -a it will give you all the hidden files.
list all files in Detail Format include Hidden Files
Then you can combine the option so whenever you give ls -a for showing the hidden file and then l for showing the long list then it will give you the hidden file and long list combined. So press enter and will give you a long list and hidden file.
Sort all Files Based on Size
You can sort your directory by their size so I will just give ls -l for a long list and then when you give capital S it will sort your directory by their size then press ENTER and you can see the file which has the biggest size is listed first and then they will go down as according to the size so whatever file has the lowest sizes will be at the bottom and whatever files or directory has the biggest size it will go at the top.
List any files by Their Extension
You want to know list something which only we know the extension. So let’s list, For example, You can just write ls documents and press ENTER and this document has this test.html and test.py file but for example, I want to only list out all the html files which are there in the document folder. So what I can do so I can just write ls documents/*.html so which will be will search for all the prefix which are there. So this means I want to search for all the files which end with .html and then I will press ENTER and it only prints out this test.html
List all Files and Directory
On the other way when you give ls *.* and press ENTER it will list out all files which are there in your documents directory right now you can also output the content of your ls to a file.
Save The Output of This Command in a File
$ls > fileName
For example, I want to save all the result which I have listed here to some files. I will write ls -lS and then I want to save all the content to a file so I can just give this angle bracket (>) and then the name of the file in which I want to save this content. So I will just say result.txt by the way this result.txt is not existing here so it will create this result.txt and then fill all the output of ls -lS command result.txt file.
So this arrow means the direction of the stream so it will put all the content into the result and press ENTER and you will be able to see we have one new file created which is result.txt and when I open this file then it will just show all the output of ls -lS command.
List all Directory
$ls -d */
The next command is to list out only the directories. So For example, You want to just list out only the directories you can just give ls -d */ and it will list out all the direct directories. So you can see it’s not listing out the files but only the directories.
So these are all the important commands related to ls but this is not all ls Command is you know having more options and flags and if you want to know more about this ls command.
What you can do just write man and then ls So man is the dictionary kind of a thing for your Linux commands and when anyway you give this man command and then give the command name which you want to see or you want to take the reference.
I have only shown you the important commands related to ls. But there is a big list which you can you know execute with this ls command. so just remember this man command if you want to take the reference of all your know references or particular commands.
HAVE A GREAT DAY