DEV Community

Marc Philippe Beaujean
Marc Philippe Beaujean

Posted on • Updated on • Originally published at byteschool.io

Unix Command Basics new Developers should know

At my first internship for an IT firm, I was very confused when asked to work with the terminal (or "shell"). It seemed very intimidating and the sure mass of different commands I was seeing people using while googling for answers to problems was very overwhelming. In this tutorial, I will break down some of the fundamental commands that you should know, so that you can have a much easier time than I did! Also, in case you didn't know: you can use unix shell command if you are working with a machine running Linux or Mac!

Why learn how to use the Terminal

Most common use for the terminal is for server administration on remote machines. This is important for developers who are running their applications on remote servers that need to be setup, maintained, etc. Another use is various development applications and tools that do not have their own GUI, so the command line is used instead (most commonly NPM and Git). Finally, it can also be much faster to work from the shell once you are comfortable with it (which will take time but is ultimately very much worth it). These aren't the only reasons, but hopefully they are good enough to keep you motivated in learning this stuff!

Breaking down a Unix Command

When we open a terminal and write something inside, our computer will try to look through a list of installed applications or commands and will attempt to initiate a sequence of processes based on that. Most commands require some level of user input, on Linux these come in the form of Options (also referred to as Flags) and Arguments. Options/flags are going to allow us to customize how the command is executed, while arguments are going to give the command vital input. We usually use options interchangeably, whereas arguments can sometimes be invalid or missing, causing the command to throw up an error. Let's take a look at a simple example, don't worry if you are not familiar with the command:

ls -l /bin

In this case, our command is ls, which lists files in a directory - try executing it and note what you are seeing. Options follow after a hyphen, in this case the option is a single l, which stands for "long" and makes sure that all files are listed in the long file format. Try removing the option and see how the output changes. The amount of available options is often overwhelming, so let's look at a site that has all the options for ls. We can see that we can also combine options, like allowing hidden files to be visible by appending the "a" character to our options. Finally, we have the argument, which in this case is just a single directory path (the directory we want to view).

Manpages

Pretty much every command has a "manpage", which stands for "Manual Page". These are designed to give you a detailed explanation of what exactly the command does and all of the flags. Let's use it on the previous command:

man ls

Entering the previous command, we can see that our terminal is now listing all the different options for the ls command, along with useful descriptions on what each does. You can scroll around or press "q" to quit the reading view once you are done.

Navigating the Filesystem via the Terminal

I am now going to give you a short introduction to how we can navigate the file system with our terminal. This is important, since most work on a server will require you to move around and inspect directories or files. Our terminal always has an absolute location, which tells us where we are located right now in the file system. We can view this location with the command pwd (short for "print working directory"). Type pwd into your terminal and you will see the directory you are currently in!

For moving around the most common command is cd, short for "current directory", which allows us to move from our current directory to another. For example, in my "Home" folder (the default directory when opening a new terminal), I have a folder called "Documents". If I enter the "cd" command and pass Documents as the arguments, you will see that your have been moved to this directory. You can also do absolute paths, by starting off the argument path with a dash (i.e. cd /bin will take you to the bin folder, no matter where you are in the file system). Here is a quick overview, you can see the terminal directory on the next to where I entered the commands:

Basic File manipulation

In unix, you can create files with the touch command, which takes the filename as an argument. Using the command cat, we can pass a filepath as the argument and have the contents of that file displayed in our console. The rm command is used to delete the file that is passed as the argument. Remember: it is always relative to the location of your current terminal. The cp command is used to copy files. You can move files with the mv command, the first argument is the relative path to the file or directory you are trying to move, the second is the target directory. You can also use mv to rename files. For this, you need to add the original file as the first argument and have the second argument be that same file only with the name adjusted at the end (e.g. mv helloworld.txt hello.txt will rename a file helloworld.txt to hello.txt). All files within a directory can be manipulated using the "wildcard" symbol *, which is used to select multiple files that follow similar patterns (useful for operations like moving or copying all files in a directory). For example, mv ./*.jpeg ./my-images will move all files ending with .jpeg into a directory named "my-images".

Viewing System Information

The command w requires no arguments and simply gives us a list of information on the user for our current session. uname displays details on the operating system we are running, but is only really useful when combined with different flags that let you output everything in a more detailed fashion (uname -a shows you the exact version of your operating system and distro, for example).

Commandline tricks to make you work faster

Your commandline's history is saved to a file, meaning you can access previous commands quickly. Press Ctrl + R to go into history mode where you can type in the beginnings of commands and have a type of auto-completion available, based on recent commands you entered (press Ctrl + C to exit it again if you cant find what you are looking). Because of this, you can also toggle between last commands very quickly using the up and down arrow keys - this will significantly increase the rate at which you can work with the terminal. I also suggest looking into how you can quickly change positions on the terminal while you are working on it i.e. hopping to the start or end of the line using 7 and 1 on the numpad respectively.

I hope you enjoyed this quick intro to the command line! If you want to learn more, want to share some of your own tricks or think that something is wrong/missing, please leave a comment.

Top comments (5)

Collapse
 
ravikrishnappa profile image
Ravi Krishnappa

GUI to CLI could confuse some newcomers.
For those users, "tree" is a good command to know. It shows the directory structure in a beautiful tree layout

tree -L 1
gives the directory structure one level
tree -L 2
gives the directory structure 2 levels

Collapse
 
mccurcio profile image
Matt Curcio • Edited

One of the first commands I was admonished NEVER to use was:

$ rm -fr *

So maybe that should be mentioned too. ;))

Collapse
 
marcbeaujean profile image
Marc Philippe Beaujean

That's great to mention, thanks for the advice :)

Collapse
 
tech2blog profile image
Tech2Blog.com • Edited

Basic commands like cat, w, id, uname, etc are missing.

Collapse
 
marcbeaujean profile image
Marc Philippe Beaujean

I added some of these, thanks for the feedback!