The Linux ls command is one of the first basic commands that a Linux user should be aware of when it comes to working in the command line. There is just knowing how to change the current working path, know what the current working path is, and also listing what is in a given path. So when it comes to listing what is in a given folder that is where the Linux ls command will come into play.
There is much more to the Linux ls command beyond just listing what folders and files are in a given path location. There is listing hidden folders and files, and not doing so, there is listing file access permissions for a file, and there is not doing so. So there is not just typing the Linux ls command in and that is that, there are a few options that one should know about when it comes to Linux ls.
Also there is of course the limits of the Linux ls command, it is not like Linux ls is the only option when it comes to getting lists of files in one or more paths. There are other commands such as the Linux find method, and also using Linux ls with other commands such as cat and grep to do a better job of finding what one might be looking for in a file system. SO lets get stared with the basics, and not so basics when it comes to using the Linux ls command.
So for this example I will not just be covering a basic example of the Linux ls command, but a whole bunch of other basic commands that come up all the time when using bash. Here I am starting out by using the mkdir command to create a new folder called foo in the home path of the current user. I then use the cd command to make the current working path this folder that I just made. In this foo folder I am using the echo command and something called redirection to create a new file with the text hello world with a file name called hello.txt.
I can not use the Linux ls command to list the contents of this folder by just calling the command without any additional arguments if I just want to confirm that I have a single file in this foo folder called hello.txt, and sure enough I do. I can not use a command such as cat to print out the contents of this file that I just made and sure enough it is what I have cerated using the Linux echo command.
So now we have the basic idea of the Linux ls command worked out. I have a folder with just a single file in it called hello.txt and when I use the Linux ls command I get that file listed in the standard output. However what about hidden files? Also is it not true that there is this thing called file access permissions? What about creating a list of files and then piping that to another command? Well with that all said maybe it is called for to look at least a few more examples of Linux ls.
So it is possible to have hidden files and folders inn a path, these folder and files names begin with a period. So if I use my echo and redirection trick to create another file but with a starting period in the file name such a file will be hidden. So if I just use the Linux ls command without any options of any kind the file will not show up.
The hidden file that I just made will not show up. That is to be expected sense it is indeed a hidden file after all. STill there should be ways of listing everything that is in a folder. So lets look at some options for listing hidden files and folders.
So there is the list all option for the ls command that will list all contents for a folder, even folders such as dot and double dot that just mean the current folder, and up one folder.
So this is a good option to be aware of, and I generally always use it with the ls command to make sure that I am seeing everything there is in a folder. However what if I just want to list hidden files? Well regular expressions come to mind so lets look at least one more example on listing hidden files and folders with the Linux ls command.
Another option for listing hidden files and folders in a path would be to get into using regular expressions. Learning a thing or two about regular expressions is not just a good idea for using the Linux ls command too, as all kinds of commands in Linux such as find and grep just to name a few also can make use of regular expressions.
The long listing format option can be used to get more detailed information about the contents of a folder. This detailed information can be used to get a better idea of what is going on with file access permissions, and other useful data about each file.
When it comes to making chnages to file access permsions then the command to use would be chmod. Use of ls -l, chmod, and sometimes sudo or su are needed to have control over file access permissions.
So one of the cool things about learning all these little Linux commands is that they can all be used together by way of piping and redirection. The Linux ls command is a great way to go about getting a list of files, but then there is piping that list of files to another command.
If I use piping to pipe the output of ls to a command like cat then it will just be the list of files as usual. This is because I am piping a list of files to the standard output of cat, rather than feeding the files as arguments to cat. If I pipe this list of files to a command like xargs then use cat with xargs this will result in the content of all the files being concatenated together. This can then be piped once more to yet even another command and so on such as the wc command that will give a word count of the text thus far.
So the Linux ls command is one of those commands that a Linus user should know about just to gain that basic degree of understanding when it comes to navigating around the command line. There is still a few things to be aware of even when it comes to just a basic command such as this. Hopefully this post has covered at least the basics of the Linux ls command, but also a bit more than just that also.