The “ls” Command – Learning How to Use Terminal (Shell) [Tutorial]

You probably don’t need an explanation on what the Terminal (Shell) is in macOS, or Linux, because if you are searching you are interested in learning it, and if that’s the case then you probably have a good reason for using it, and you’re not such of a novice in general.

But for summarizing in simple form, a terminal emulator lets you use the command line for doing different task, most of the time easier and more simple. Furthermore it’s not that resource hungry on graphics as a normal GUI of a modern OS, and it’s more powerful in many cases, that’s for sure.

Command line tools let you do different things, and they are the preferred way for web developers and linux server administrators to do their job. As a developer your work will be simplified if you harness the power of the command line.

You can learn the command line in many ways and places, and this is just my take on a simple, easy and fast way for you to get started as a newbie to use, learn and understand the command line at a basic level.

So let’s get started!

I am going to use the Terminal app provided in macOS for this lesson / tutorial.

Terminal

Regardless if you are on a Linux based distribution or a Unix based OS, you’ll probably see at a certain point the word Bash somewhere when you are using the Terminal.

Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been distributed widely as it is a default shell on the major Linux distributions and macOS.

So the most basic things you probably do with is navigate and manipulate items in your file system. You delete, move, rename, create files and directories, etc.

In macOS, when you open a terminal app window, it defaults to your user directory.

To see a list of the files and folders (aka directories) in your user folder you have to use the ls command.

terminal-ls-command

ls  command – lists directory contents of files and directories

You can use the commands individually and usually they apply system wide or on the directory you are currently in. Also you can use them with options and furthermore with files and directories. And also a combination of both.

The syntax for the command is:

So you can do things like:

terminal-2016-09-15-at-15-27-07

As for using the command you can specify a directory, regardless of how deep it is. That means, you can list the contents of a folder within a folder. You can list it if it’s in the same folder as your user folder, if it’s in the same folder of the parent of your user folder and so on. Any folder / directory, anywhere.

..  tells your system to go up to the directory immediately above the one in which you are currently working:

If a folder’s name contains many words, you can substitute the space character with \ :

ls  command options:

 

terminal-2016-09-15-at-21-57-03

So here is some more stuff you can do with ls :

$ ls > out.txt #redirects to output file out.txt $ ls -d $PWD/* #list files and dir with full path

Other useful  ls  command examples:

The ways you use this command is similar to how you will use other shell commands, because of the similar syntax, the common options and ways of referencing files.

A very common use for the ls  command is for showing file permissions. It comes really handy when working with server files.

If you want to learn more about the Terminal (Shell), I wrote a nice article here, where I present the cd  command (changing directories) and about absolute and relative paths. Check it out!

Hope you enjoyed this tutorial, and found it useful! And I’ll be doing more on this subject in the near future therefore don’t forget to subscribe to my Newsletter to get the latest posts! Also follow me on Social Media!

Thanks,

signature

1 Comment

Leave a Reply

Back