Jump to content

Linux Commands For Beginners: LS


emerglines

Recommended Posts

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!


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

  • List files and show permissions, user owner, group owner, last modified/created date

[[email protected] $] 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

[[email protected] $] l ls -R

  • List all files, sorted by file size

[[email protected] $] l ls -S

  • List only directories

[[email protected] $] l ls -d

  • Human readable – Size in MB or GB

  • Sort in alphabetical order by file extension

Now Let’s See Some Combinations

  • List “all” files, in “human readable format” in long detailed format.

[[email protected] $] ls -alh

  • List “all” files and output the contents to a text file

[[email protected] $] ls -a > contents.txt

NOTE: > 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 protected] $] 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!


Source

Edited by emerglines
Link to comment
Share on other sites


  • Replies 4
  • Views 1.3k
  • Created
  • Last Reply

Top Posters In This Topic

  • tsaqofa

    2

  • TuFaS

    1

  • emerglines

    1

  • FukenGruven

    1

Top Posters In This Topic

  • 4 months later...

i've tried to install Linux (Ubuntu) twice..

both times i was stuck when it kept asking for a terminal response..

anyone know what the heck it's asking for?

and how i can successfully pass that screen?

Link to comment
Share on other sites


i've tried to install Linux (Ubuntu) twice..

both times i was stuck when it kept asking for a terminal response..

anyone know what the heck it's asking for?

and how i can successfully pass that screen?

Can you send screenshot related installation problems

If there is an image may be more obvious to seek a solution

Edited by tsaqofa
Link to comment
Share on other sites


Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...