For some of you, this is a basic command that you use literally all the time, If this is you, then this tutorial isn’t for you. But if you’re new to Linux or just new to the command line “ls” is a command you will get to know very well. At the surface level, ls is a simple command, and if typed in any directory or anywhere on the command line, all it does is list the contents of your current working directory. But if I were to leave it at just that I would have done you no favors at all. In fact, I’m going to tell you some tips and tricks to ls and we might even throw in a grep command if you’re lucky!
Learn how to make a Minecraft Server step-by-step!
LS – 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
[firstname.lastname@example.org $] ls -a
- List files and show permissions, user owner, group owner, last modified/created date
[email@example.com $] 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
[firstname.lastname@example.org $] l ls -R
- List all files, sorted by file size
[email@example.com $] l ls -S
- List only directories
[firstname.lastname@example.org $] l ls -d
- Human readable – Size in MB or GB
[email@example.com $] ls -h
- Sort in alphabetical order by file extension
[firstname.lastname@example.org $] ls -X
Now Let’s See Some Combinations
- List “all” files, in “human readable format” in long detailed format.
[email@example.com $] ls -alh
- List “all” files and output the contents to a text file
[firstname.lastname@example.org $] 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
[email@example.com $] ls -a | grep *.txt
There’s More But Start Simple
Take a look at the man file for LS “man ls” and you’ll see many more options. But at the risk of overwhelming you I just created the most common LS commands/flags with a combination. This is a practical usage guide for beginners on how we use the LS command on a regular basis. 99% of the time this will be all you need, and if you need more, well then you’re not a beginner and this tutorial isn’t for you!