DEV Community

Stephan Bakkelund Valois
Stephan Bakkelund Valois

Posted on

Beginning Linux and Mac terminal

For the complete beginner, and for those who just need a quick refresh. If you use Linux as your primary OS, this blog post is probably not for you.

If you’re thinking about doing any sorts of computer programming, development, or working with servers, you probably should get used to working with in a terminal window. Even if you’re just tired of Windows, switching to Linux could be very good for you.

Now, if you want to work with Microsoft products, that might be a different story. I’m not too familiar with the .NET platform, so this post won’t touch on any of that.

There are an incredible amount of software out there, created to help programmers build cool stuff. Most of them are Command Line Applications. You do have some programs that comes with a GUI, but most of them started out, or also have a Command Line version of the application.

Most of the things you do in a GUI environment, you can do in a terminal. Be it working with files, downloading from the internet, writing, and so forth. This blog post focuses on the basic terminal commands. It won’t make you an expert, but it will allow you to navigate, and work in the terminal with confidence.

PS: If you don’t have access to a linux terminal, you can find many online terminals to play around with for free. For example https://cocalc.com/doc/terminal.html

ls

Short for list, this command lists all files and folders in a directory.

~ $ ls
file.py ingredients.txt secret_folder

As we can see, our current directory consists of 3 items — 2 files and a super secret folder. we can have a peak into the folder from our current directory. Since the secret_folder is inside our current directory, we can use the ls command, followed by the folder name

~ $ ls secret_folder/
nintendo_cheat_codes.txt

Cool. the secret_folder contains a file. Now, how can we navigate into the folder?

cd

This command is short for change directory. At a basic level, you can use it the same way as ls. You enter cd, followed by the directory you want to navigate to

~ $ cd secret_folder/
~/secret_folder $ ls
nintendo_cheat_codes.txt

So we cd into the secret_folder directory, and run the command ls to list the files inside of the directory. What if we want to go back to the previous directory?

‘..’ and ‘.’

Dots? Well, yeah. Those dots have a special meaning in a terminal window. one dot means this current directory, while two dots means the parent directory, or the directory your current directory sits in. The two dots will help us navigating to the previous directory

~/secret_folder $ cd ..
~/ $ ls
file.py ingredients.txt secret_folder

So now we know how to change directories, and how to list the files and folders inside of them. If you ask me, running these commands are a lot less work than physically moving your hand over to your computer mouse and clicking stuff!

pwd

So, how can we keep track on where we are, relative to the root? The pwd command is short for Print Working Directory and will show you exactly where you are.

~ $ pwd
/home/myuser

mkdir

How can we create new folders? We can use the mkdir command, which stands for Make Directory. We type in the command, followed by the name of our new directory

~/ $ mkdir not_so_secret_folder
~/ $ ls
file.py ingredients.txt not_so_secret_folder secret_folder

As we can see by listing the items in our current directory, our newly added directory got created.

touch

This command is probably the easiest way to create any file you want. You type in the command, followed by the filename WITH the file extension.

~/ $ touch new_file.txt
~/ $ ls
new_file.txt

rm

If you want to remove files or folders, there are a couple of commands you can use. The easiest one is probably the command rm, which stands for Remove.

~/ $ ls
new_file.txt
~/ $ rm new_file.txt
~/ $ ls

So if we list our files, we can see the file we just created with touch. By using rm, followed by the filename, we’ll delete the file. What about folders?

~/ $ mkdir to_be_deleted
~/ $ ls
to_be_deleted
~/ $ rm to_be_deleted/
rm: cannot remove ‘to_be_deleted/’: Is a directory

We get an error. In linux, we are not able to delete directories! Well, that’s a lie. We just need to use a flag! What is a flag? A flag is a special command we put alongside the main command, to alter the way the main command executes. We won’t go through every type of flags in this post, but let's take a look at the flags we need to delete a directory

~/ $ ls
to_be_deleted
~/ $ rm -r to_be_deleted/
~/ $ ls
~/t $

The -r flag tells the rm command to delete the folder and all its content. It works like a little safety net. Deleted files are usually not recoverable. Sometimes, you might have problems deleting files because of file permissions and such. There is another flag you can use, but must always be used with extreme caution

rm -rf

This command works the same as the previous command, except it will delete everything, no questions asked. If you were to write rm -rf *, you could, in a worst case scenario, do extreme damage to your system. I wanted to put this in a separate section because of this. Don’t be afraid to use the command, however. I use it all the time. Just be sure you know what you’re deleting, you probably won’t be able to recover the files.

cp

Sometimes, you want to copy stuff from one place, to another. The command cp, short for copy will do just that!

~/ $ ls
file.py ingredients.txt not_so_secret_folder secret_folder
~/ $ cp ingredients.txt not_so_secret_folder/
~/ $ ls not_so_secret_folder/
ingredients.txt

So, we list our files, and decide to move the ingredient.txt file into our not so secret folder. we do this by using the cp command, followed by the path, or folder we want to move the file to.

mv

Now, we copied the file to the wrong directory! Do we have to delete it, and copy the file to the proper directory? That sounds like a lot of work! Well, we could use the mv command, short for Move. The way mv is used, is pretty similar to the cp command. we use mv, followed by the file that we’re moving, and its destination.

~/ $ mv not_so_secret_folder/ingredients.txt secret_folder/
~/ $ ls not_so_secret_folder/
~/ $ ls secret_folder/
ingredients.txt nintendo_cheat_codes.txt

So we gave the command two paths. The first path is to where the file is. The second path is where we want to put the file.

Rename files and folders?

Would you believe me if I told you that there are no command for renaming files and folders? It’s actually true. There is an easy way of doing it, however it does not have its own command. We need to use the previous command, the mv. What the command actually does, is delete the file, and recreate it at the chosen path. We can take advantage of this, and use it to give the file a new name.

~/ $ touch filr.txt
~/ $ ls
filr.txt
~/ $ mv filr.txt file.txt
~/$ ls
file.txt
~/ $

In this example, we created a new file, but made a nasty typo. No problem! We just used the mv command to rename the file to its proper name!

Tab button in the terminal

To save time, the tab button is amazing. It can work as an auto complete, and also as a lightweight ls, when used with other commands. Say you want to copy a file to a new directory, but you suddenly forgot the exact name of the directory. Let’s discuss two ways of using the tab button

~/ $ cp
file.py ingredients.txt not_so_secret_folder/ secret_folder/

By writing cp followed by a space, and pressing tab twice, all the files in my current directory gets listed. Say we want to copy file.py into not_so_secret_folder. It was quite a long name, so we used the cp command to list the directory, and we start writing the name of the directory

~/ $ cp file.py no

We press tab once

~/ $ cp file.py not_so_secret_folder/

and the rest of the directory name gets autocompleted. The tab button is super useful for quick navigation through the file system.

cat

Not the cat as in the purring kitty, but short for concatenate. This command can do multiple operations. One of the most useful ones, is printing a file content to the terminal, in one quick motion.

~/ $ cat ingredients.txt
1 tbsp of something
2 cups of something else

we type cat, followed by the filename. The content of the filename gets printed to the terminal. This is a very quick way to peak inside the content of a file, without opening it up in any kind of software or text editor. (This is a side effect of the cat command. We won’t go over the concatenating part of the command in this blog post)

Text editors in terminal?

For editing text, there are a couple of editors that are pretty standard in every terminal you come across. Nano/Pico, and Vim/Vi. Nano is a simple text editor for manipulating and editing text. Vim is a workhorse (and my personal favorite) which, with some modifications, can do pretty much whatever you want it to do.

If you’ve never used Vim before, Nano is a whole lot easier. It will show you its commands in the lower part of the program, and you can navigate the text by using the arrow keys.

For Vim, it’s a whole different story. It comes with its own key bindings, it own way of doing.. Pretty much everything. I would definitely recommend learning it. However, once you learn how to use it, you will be annoyed that other writing softwares don’t use the same key bindings. I might write a beginner tutorial for Vim if anyone wants, however you’ll find tons of tutorials if you google it. Leave a comment if it sounds interesting.

Last words

These commands should get you up and running. There are a ton of other commands to pick up, but it should not be a problem once you learn the ones listed above.

If you don’t use any special Windows software, or play high end games, you might just migrate over to a more secure, and stable OS. Not to mention, most of the Linux distros are free. And most of the software that runs on Linux is free.

PS: If you like playing games, Steam supports a huge amount of games on Linux!

In dire need of Microsoft Office suite? You can use Google Suite or Libre Office. You need Photoshop? What about the free photo editor Gimp! You’ll most likely find a free, amazing replacement for all your Windows software.

Best Regards
Stephan Bakkelund Valois

Top comments (0)