Last Updated: February 14, 2022
·
68
· janiali

LS - The Linux Basic Command – More Than Meets The Eye

LS lists the contents of your current working directory. This is very simple if you have a directory with a few files in it and hard to use if you have a directory with hundreds or even thousands of files in it. Not only that, by default Linux hides all files that start with a dot (.) because Linux interprets these as configuration files, and only power users, or users who really know what they are doing should see these on a regular basis. Simply using the ls command on the command line won’t actually display these dot files and if you’re a Linux beginner this might be frustrating.Think you’re done? Perhaps we have a few other use cases that you didn’t think about. For example, what if you’re looking to list all files that have a file extension of .txt? Or maybe you just want to list all the directories but not individual files? How about if you need to output the standard out results to a text file? Or if you need to see the size details, ownership details, date modified, permissions, and the size in megabyte format? Heck we can even list all the files that are located inside of the files that we’ve just listed (recursive)!!Don’t worry if your mind is blown, then we just need to get to the examples. Your take away? Knowing the ls command will only get you sar far, reading the man page will get you a little farther, and this tutorial will take you even farther than that. Will it teach you everything about the ls command? No, the fact of the matter is that’s why Linux is so deep and flexible. If you think you’ve done it or know it, there is ALWAYS way more to everything, even the most simple commands. This is why Linux is the #1 operating system for servers and UNIX/Linux is found on 90% of all smart phones (iOS is UNIX and Android is Linux). Don’t let it intimidate you–it will only make you smarter.

Examples & Use Cases
List all files in a Linux directory including hidden (dot) files and directories
[janiali@linux $] ls -a

List files and show permissions, user owner, group owner, last modified/created date
[janiali@linux $] l ls -l-l stands for “long listing” and will show you all the details important to the Linux system about the Linux file.

List all files, and all the files inside of the directories (or just list the folder recursively
[janiali@linux $] l ls -R

List all files, sorted by file size
[janiali@linux $] l ls -S

List only directories
[janiali@linux $] l ls -d

Human readable – Size in MB or GB
[janiali@linux $] ls -h

Sort in alphabetical order by file extension
[janiali@linux $] ls -X

Now Let’s See Some Combinations
List “all” files, in “human readable format” in long detailed format.
[janiali@linux $] ls -alh

List “all” files and output the contents to a text file
[janiali@linux $] ls -a > contents.txtNOTE: > will create a text file or write over the existing file >> will append to the file if it exists or create the file if it does not.

List “all” files but show only those files ending with .txt
[janiali@linux $] ls -a | grep *.txt