Understanding & Executing - Linux Command Line - Part 1

The first part of a walkthrough of the Linux (& macOS) command line

Andreas
9 min readMar 11, 2018

If you’re running Linux or macOS, as a developer learning the command line is practically essential. The terminal is one of the most powerful tools you can have on your system. However, learning how to actually use it can be somewhat difficult if you don’t know where to look or if you don’t have a decent understanding of how it works to begin with.

So what is the command line shell?

First we need to look at the kernel. The kernel can be explained as the hub of your OS. In other words, the kernel handles system calls & allocates time as well as memory to programs on your computer.

Now the command-line shell is a user interface between the user and the kernel. Here’s an example of the command line & the kernel working together; Let’s say you want to make a file called commands.md, this can be done with the touch command.

touch commands.md

Now say you enter this command in to your terminal and hit enter to run it; At that point your command line will send a request to the kernel through system calls to execute the command you just entered into your terminal. When the process is finished the shell will return the shell prompt

$

to let the user know (in this case) that the file has been created & that it’s ready for another command.

As a user you can customize your shell and even have different ones on the same system running at the same time. The default shell is called bash, but I personally use zsh (which is why my terminal may look a bit different in the screenshots); There are lots of different shells & they all have their own special features. (I will tell you how to see what shells you have installed & how to switch between them later on).

Before we get into using the commands we’ll have to look at the directory structure. Most Linux systems uses the Filesystem Hierarchy Standard where the top of the hierarchy is known as root (and is written as a slash / ). From root you can find all files on your system that you normally can’t see in your finder/file explorer. But to keep things simple, this is the location of your Desktop

/Users/<user>/Desktop       //macOS
/home/<user>/Desktop //Linux

Just to be clear here, the file we created commands.md will be created at the directory you’re currently located in. So if you were to run it while located at your desktop, the commands.md file will appear at your desktop.

Command Flags

There’s one more thing I need to mention before we get into the commands & that’s the flags. Flags are different options we have that allows us to set one or more conditions on the command we’re executing. Flags are in other words, used to set conditions on how the output should look and what the command should do in a more specific manner. You always put flags after the command itself with a character/word that sets the condition.

ls -l

In the example above, -l is the flag for the ls command specifying that the output should contain more information & be listed vertically rather than horizontally. You can also put flag characters right next to each other to use several flags at the same time.

ls -alh     //here we're using the tree flags a, l & h

Keep in mind I will not mention all the flags for the commands I mention here (as there is quite a lot of them) and the flags availability will change depending on which command is executed. Flags are also case sensitive so

-a

will for example not be the same as

-A

One more thing, you can usually use

 man -c <command>

to get an explanation of the command along with all the flags and what the flag does (You need to press q to exit the window this command will open).

The Basic & Common Commands

Listing contents of directories

Alright so starting off we got the ls command that I mentioned above. ls stands for list, which does exactly what it means. Executing this command in your command line will display the contents (files & directories) of the directory you’re currently in. Entering a path after the ls command will let you execute the command outside of a directory you’re not located in. You can also use the -a flag to see hidden content (such as files starting with a dot . )

ls -al /Users/<user>/Desktop

Changing directories

Next we got the cd command that stands for change directory. This is how you navigate around your directories in the terminal. Remember how I mentioned root being written as a slash / ? Well, if you type in

cd /

you will be moved to the root directory. You can also use

cd -

to get back to your previous location (if you for example would accidentally swap directories).

Your location in the file system

You can see which directory you’re located in using the pwd command.

pwd

Here’s a screenshot of me using the commands we’ve gone over so far for you to see how it really looks in action.

The blue text is the name of the directory I’m currently in

You can ignore the additional information displayed when we run the ls command for now. Let’s focus on the dots that appeared when we ran the ls command the second time.

The first dot . is the path to your current directory. Therefore if you where to cd to one dot you will always end up in the same directory (so cd into it is useless). However you can use this for example when you’re coding and want to specify the path of a file in the same directory as the code file. (It’s also useful for other commands such as the find command that I will go over later on).

background-image: url(“./iPhone.jpg”);

However the two dots .. are a bit more interesting as they represent the parent directory of the one we’re currently in. In this case, that would be the Desktop.

Creating files and directories

I already mentioned the touch command earlier, but I’ll include it here regardless. The touch command allows you to create any type of file you want. Simply enter the name of the file with the file format at the end (such as .txt). You don’t need to enter a file format, and you can make it “invisible” by putting a dot . before the name.

touch .superSecretFile

The command for creating directories is mkdir which stands for make directory. And just like with the touch command, you simply enter the name after the command.

mkdir <folderName>

Deleting files and directories

Deleting files and directories is just as easy as creating them. The command to delete a directory is rmdir which stands for remove directory. You use this the same way as you use the mkdir command.

rmdir <folderName>

But, if the directory has contents in it, it will show you an error telling you it’s not empty. To ignore this error and delete everything in the directory you use the rm command, which is also the command you use to delete files.

rm <fileName>rm -rf <folder>

For the folder we’re using the r & f flag. The f flag stands for force which basically makes the command ignore the warning you would normally get & the r flag stands for recursive which will make the command go through each folder recursively until it hits the end of the directory tree.

I do suggest caution using the r & f flags when using the rm command as you can do some serious damage with it. And I personally don’t know how easy it is to recover the files you delete.

Standard Streams

When it comes to having information displayed to your screen or when you enter a command your computer does this through something called standard streams. So when you enter a command it goes through Standard Input (stdin) which is then processed & sent back through Standard Output (stdout) or Standard Error (stderr).

Displaying file information & content

The cat command can be used to read one or several files, which will then be printed to your screen through standard output.

cat file.txt   //Printing out one file
cat file1.txt file2.txt file3.txt //Printing several files at once

Another way to display file contents to the screen is using the more or less command. The more command is used when printing a file with too much content to print on one page, splitting it up to several pages. Simply put, you can scroll down but not up. The less command works essentially the same way, it has a few more features, but I wont go over them here as I don’t want to make this part excessively long.

more file.txt
less file.txt

A quick note on the more/less commands; On some systems the more and less commands are “merged” making both commands practically the same so you wont notice a difference really.

Now if you only want to see the very top of a document you can use the head command, which will show the first 10 lines of the document. The tail command will do the same thing as the head command, except it will show the first 10 lines from the bottom up.

head file.txt
tail file.txt

Then of course we got the echo command which let’s you enter text into a string which will then be displayed to your screen through standard output.

echo “<string>”

The quotation marks are not necessary but will not be displayed if you wish to use them.

Moving & Copying files

To copy a file you use the cp command. You must specify the target file & directory you want to copy the file to, if the target file doesn’t exist, the system will create a new file with the name you tried targeting.

Moving a file can be done using the mv command. Just like with the cp command, you will need to specify where you want to move the file. Moving the file to the same directory the current file is already in will rename the file.

mv <file> /<directory path>/
mv <file> <newFileName>
cp <file> /<directory path>/
cp <file> <copyOfFileWithAnotherName>

Searching & Locating

So let’s say you’re looking for a specific content on your system following a specific pattern such as name/size/type/permissions etc. There’s a command that allows you do exactly this called find. For this command I will also show you how useful flags can be.

So the find command recursively goes through your file system looking for content and prints the files through standard output. Here’s a simple syntax of the find command to files on your system

find <path> -name “name.file”

The path is where you specify the find command should look for the file. This can be anything from root to a specific folder.

Let’s look at a few more examples

find ~ -size +10M 

Here the find command will search for all content starting from the home directory (~) that has a size above 10MB. This is an example of the size flag, the +10M specifies the memory of content to look for. (I will go over memory suffixes in part 2, this was just an example of using flags)

find . -type d -maxdepth 1 

In this example we use the flag -type to tell the find command to only look for directories ( d = directories), the -maxdepth flag tells the find command to only look one step down in the filesystem.

find . -name “d*” -exec ls -l {} \; 

In this example the -exec flag runs what comes after it if/when the first requirement is fulfilled, the curly brackets {} specify to target all files.

So we’ve gone over how to find files and directories, but what if you want to find a specific word or sentence in file? This can be done with the grep command. The syntax of grep looks like this

grep <word/pattern of characters> <file(s)>

An example of this would be:

grep software documentation.txt

Useful flags:

grep -i (case insensitive search)
grep -c (number of lines that matches)
grep -v (returns the lines that don’t match the pattern or words)
grep -l (returns the name of the file if the file matches the words or pattern)

Remarks

So that makes the end of part one, I hope you learned something and found it interesting. If so, please consider sharing it to friends & family who you think should read this ❤

Now, why split this into two parts?

First of all, this post became a lot longer than I expected and reading while retaining all the information from a text is only possible for so long. If this is all new territory for you, a break is probably necessary to remember everything you just read. I also encourage you to (if possible) test everything you’ve learned & play around with it to make sure you remember it. The next part will also be a little bit more advanced as so far we’ve only looked at the basics.

Part 2: https://medium.com/@AndreasSterneer/unixcommandline2-86c94c94e0a4

--

--