As developers, the terminal can be our second home.
However, we can’t use it until we learn how to, and need to practice using it to learn— it’s a bit of a catch-22.
I hope this introduction will solve that puzzle for you. That, after reading this tutorial, you will be able to start making use of the terminal right away.
I’ll cover the basics first, so if you know all the things in this small bash tutorial stay tuned for the next ones, where I’ll tackle more advanced topics.
With that taken care of, I’ll start from the very beginning. If you’re on Ubuntu, all you have to do to open your terminal is press ctrl+alt+t. On a Mac, you should press cmd+spacebar, start typing terminal and press enter when the option appears.
In both cases, you should see a dark background with your username followed by your computer’s name (in Linux) or the reverse order (in a Mac).
I strongly advise you to open your own terminal and try these commands out on an empty directory, to see for yourself and get the hang of them.
You’ll see a prompt inviting you to type commands. To enter a command just type it down and press enter. Some navigation commands are:
cd : Moving your working directory.
cd <relative path>
This will make your terminal point to a different directory, from which you can run new commands. For instance, if you’re in a folder called animals with three folders cats, dogs,and turtles, you’d run
to move into that directory. To move up one level from the current directory (e.g., moving back to animals from turtles), hit
mkdir and touch: Creating folders or files.
If you need to create a new, empty directory, all you have to do is run
mkdir <directory name>
will create an empty file in the current working directory, with the first argument as its name.
If another file with the given name already existed, this will only update the file’s last update date. It will not make any changes to its content.
‘But could I possibly know if the file exists?!’ You ask. Well, I’m glad you’re asking.
ls : See a directory’s contents.
The ls command lists the name of every file and directory inside the current working directory, in alphabetical order. You can pass it a few arguments by using dashes, like this:
ls -a -l
In this case, the -a argument makes ls show invisible files. The -l command makes the output look like a list. It displays one row for each item, with some extra data like the size of each file or its creation date.
One of my favorite arguments for ls is -R, which recursively calls ls on each listed subdirectory for a quick look into a repository or file tree.
Note that for all commands, arguments can actually be combined after a single dash:
Now, I hear you asking ‘How in the world am I going to remember all of these arguments and options? Do all commands have so many crazy features?’
Don’t worry — I got you covered.
man : Never stop learning!
If you’ve been in Stack Overflow or Reddit, you’ve probably come across the phrase ‘read the man pages’ used either educationally or as an insult.
I’m here for the first use.
man <command name>
It will display that command’s man page — official documentation, with all of its possible arguments and uses. Most of us use it when we’re sure a certain program did something, but we can’t quite remember which flag made it do it. It’s also very good to call man on a command the first time you use it (for instance, if it shows up in a google result), to learn a bit more about it and maybe find better ways to invoke it. To close a man page, just press Q.
head and tail, cat and less : Read a file’s contents.
Calling head or tail on a file will show you its first or last 10 lines, respectively.
Some cool arguments you can call it with are:
- -n <number>: display number lines instead of the default 10
- -f (for tail) : Show the lines in real time and don’t stop (perfect for keeping tabs on a log file when you ssh into a server)
Calling cat will simply display a file’s content. Make sure you’re using it on actual text files, or you’ll see some trippy stuff.
If you call cat on a large (or even large-ish, to be honest) file, you’ll probably find it pretty awkward to keep scrolling up and down, looking for the relevant lines. There’s actually a more convenient way of doing that: the lesscommand.
less will show you less of a file by loading its contents in a buffered way. You can scroll the file with the arrow keys instead of using the mouse wheel/touchpad, which is a lot more comfortable. You can also press /, type something in and press Enter to search the file (like using ctrl+f).
To exit less mode, just press Q.
cp and mv : Copy, cut and paste.
cp (copy) and mv (move)are the bash equivalents to copy and cut, respectively. You can use them like this:
cp <source> <destination>
To copy the file(s) in source to destination.
The source can either be a file, or a set of files. To select more than one file, you can leverage bash’s wildcard character: *. This character will match any string, even an empty one.
For instance, this command will copy all files in the some_folder folder into the some_other_folder folder, situated one level upwards in the file system.
cp some_folder/* ../some_other_folder
But if we wanted to only move the .txt files into a directory called texts, we’d use:
cp *.txt texts/
since * matches any string, the suffix is enforcing its ending in .txt.
For instance, *.txt matches filename.txt, since * matches filename, but not filename.xtt, since even though * matches the whole name, there’s nothing that matches .txt).
The destination can be a file’s path (overwriting the current file in that path, if it exists, or creating a new one otherwise) if the source is a single file, or a directory name if you wish to copy/move many files.
rm : Deleting files and directories.
The opposite of touch, rm deletes a file or directory.
Using it in its default form
will work when deleting a file, but throw an error when deleting a directory. This prevents us from deleting important files in a directory, or a whole directory thinking it’s just a file.
To bypass this, if you feel courageous, just add -r, to recursively delete every file in a directory until it’s empty, before deleting it like some kind of serial deleter.
If you only feel like deleting the empty directories, you can use -d instead.
Note that you can always use the wildcard (*) character to delete many files or directories in a single command. For instance, calling
removes all text files from the current working directory.
Whew, that was some introduction. You’re now familiar with the most common commands you’ll be using in your daily programming life.
There are a lot of things I didn’t cover yet. I plan to make a follow up with more use cases, more commands and more real problems to solve.
While I prepare the next article, I’d like to encourage you to try these commands on your own. See which ones save you time, and get used to this whole terminal thing. Maybe bookmark this article and use it for reference. I won’t tell anyone.
I promise you, after a while you’ll start to see why it’s worth it (I know it took me a while).
Eventually, you’ll just instinctively open the terminal every time you start doing something.
- Managing Files, Text and Strings with bash is this article’s next part, where you’ll learn a lot more useful commands, and how to combine them for infinite power.
- How to Start Using Vim on which I’ll teach you how to use the text editor for developers.
I hope you found this introduction useful, and if so please let me know! I value my readers’ feedback a lot.
This is the main reason I’m writing, so please tell me if some part was hard to understand, some commands seem useless, or my tutorial is simply too boring. Also let me know if some part was interesting!
I am sorry that this post was not useful for you!
Let us improve this post!
Would you tell me how I can improve this post?