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.
To list folders only with the ls command I will need to use the -d option, however on top of that it seems that I also need to given something for a path.
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 glob patterns. Learning a thing or two about glob patterns is not just a good idea for using the Linux ls command too, as all kinds of commands in Linux also use globs as a way to do basic pattern matching type tasks.
There might be some limitations to glob patterns though which will give rise to looking into other options such as piping output from the ls command into something that can be used to preform more complex pattern matching such as the grep command.
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 changes to file access permissions 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.
There are at least a few things that can be done with glob patterns using just the Linux ls command alone. One that I find myself using a lot is the asterisk, bit of course there are a few other options such as the range option that can be used with square brackets. In this section then I will be going over a few quick examples that have to do with creating a bunch of files that follow a given pattern and then using just the Linux ls command alone to list just a few files with a given range of numbers.
Here I have a quick bash script that will create a bunch of files
Use chmod to make the script executable
So I can use my create files script to create a bunch of text files, now I want to use the ls command to just list files that have a number between 1 and 3 for the first digit.
This works okay so far but there is a limitation with this such as the question of how to go about also listing the file that ends with 10 without listing everything else. Also when it comes to creating a whole bunch of files like this it might not always be a good idea to name them this way if I have control over doing so, as it might be better to pad files with zeros.
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.