Introduciton
It's a well-known fact that developers use terminals. And using a terminal is something that everybody should know. It is a good thing to know and do because it will help you in so many ways. It will help you in that job interview, or just in your everyday life. You must at least have the basic knowledge of how to use a terminal. For example how to change directories and view them and how to create a file.
In this post, I am going to show you some of the basic commands and how to get around in a terminal and not be completely lost.
1. pwd
pwd
means Print Working Directory. It shows you the full path to your current directory. This is used so you don't get lost in all those directories that you have. If you don't know what a directory is, it is pretty much like a folder. It stores other folders and files in it.
pwd
/Users/user1
2. ls
The ls
stands for List Files. It will show you the files and directories that are in your current directory.
ls
xmen avengers justiceLeague
3. cd
Now that you know how to list your files in your working directory and how to print the path to it, you will need to Change Directory. This is done by typing the cd
command. You just have to type it and then next to it type the directory you want to visit. But this command only moves forward and backward. This means you can't just type a directory name that is deep inside your files just by typing its name. You have to type the full path to that dir.
cd avengers
If you want to go in multiple dirs, you need to use /
after every dir, and then type the directory you want to visit inside the previous one.
cd ironMan/suitMark85
If you want to go back one dir, you need to type cd ..
. You could mix this with other dir like this:
cd ../../captainAmerica/steveRogers
Or if you just type cd
by itself, it will send you to the home directory.
4. mkdir
Now that we know how to go to a directory and how to view what's inside them, we need to learn how to make one. It is done through the mkdir
command. You just type the command and add the new directory name next to it.
mkdir secretDir
If you want to create multiple dir at once, just type all of them next to each other.
mkdir secretDir1 secretDir2 secretDir3
When doing these kinds of things be sure to use the pwd
command so you know in which dir you are so that you don't create the new dirs in the wrong one.
5. man
The man
command is used to check the manuals of a certain command. When you are not sure what a command does and what to have a detailed look at it, just type the man
command and add the other command next to it.
man ls
When you are done viewing the manual, press q
so that you can leave the file.
6. touch
The touch
command creates a file (or multiple). It is almost the same as mkdir
, but it's not used for making directories but for files with an extension(.txt, .html, .css ...).
touch index.html style.css app.js
7. nano
nano
is a text editor using a command-line interface. When you create a file and want to add something small to it, just use nano
to add it.
In order to edit a file, just type nano
and the file name next to it.
nano index.html
Note that if there isn't such a file in your directory, for example, the index.html
one, it will create a new one. So this means that you wouldn't need to use the touch
command. bUt it's best to create a file with touch
and then go to your code editor and do your coding there. nano
is best used for those minor changes like for example a typo.
There are some shortcuts that would be nice for you to know. I would recommend checking this post out on some of those shortcuts.
11 Nano shortcuts that you should know
8. rm
Now that we know how to view dirs, create them, create files and edit them, we need to learn how to delete them. Deleting a dir or file is done with the rm
command. You definitely want to be careful with this, because if you delete a file or dir, you won't be able to get it back. So if you want to delete a file just type rm
and then the file name. Or if you want to remove multiple files, just type their names next to each other
rm index.html style.css app.js
Now if you want to remove a directory you can use rmdir
, but this will only remove a dir that is empty. In order to delete a dir that is not empty, use the rm
command with the flags -rf
. If you don't know what flags are, think of them as extra options for your command. The ls
command has lots of flags. I would recommend that you check the manual of the ls
command so that you can see all its available flags.
So deleting a directory with other dirs or files inside should be done like this:
rm -rf avengers
Be very careful with these commands because if you delete a file or dir by accident, you won't be able to get them back
If you want to see some examples and a more detailed explanation for some of these commands be sure to check out this video:
Conclusion
Knowing how to use a terminal is crucial. It is. a huge bonus to know at least these commands, which will help you a lot.
I hope that this post has helped you and that you will be a little bit more confident the next time you see a terminal.
Top comments (3)
touch
is actually for updating the access and modification times of the given file or files to the current time. The fact that it creates new files if they don't currently exist is merely a convenient side effect - it is not the command's primary purposeI wrote an entire post about that once :)
I didn't know that. Thank you for clarifying!