Let’s learn the Fundamental Command-Line Commands on Linux6 min read

In past few articles, we’ve discussed Linux in general such as why do programmer prefer Linux and why almost hackers choose Linux instead of Windows or macOS. If you want to learn more about these things, check out these articles:

When you choose Linux as an operating system you work with. Inevitably, you have to communicate with command lines all the time, it also supports GUI (Graphical User Interface) either but CLI (Command-line Interface) is much powerful for professionals. If you just get into Linux and curious to learn about the most fundamental and important skills – command line – this article is for you.

Terminal (Linux Shell)

Perhaps you’ve seen many times this keyword on your computer or somewhere not just on Linux it also appears on Windows and macOS? But what does it actually mean and why is it essential? Basically, a terminal is a program that receives commands from the user and gives it to the OS to process, and it shows the output. Linux’s shell is its main part. Its distros come in, again GUI, but basically, Linux has a CLI. If you are confused about CLI, check this link.

Those commands listed below not just only work on Linux machine, it also works well with any UNIX-based system (by the way Linux OS is based on UNIX – the multitasking operating system created in 1970), and notifying that MacOS is also a UNIX-based system, so the commands below work perfectly with the Terminal in Mac as well.

Basic Linux Commands

Now, open your terminal and do some practice, hereby I use iTerm for instructions, which is quite similar to Terminal in Linux and macOS.

ls

ls is shorted for “list”, this command will give you a readout of all files and folders in your current directory. Here is an example, now I am in my directory named macbook, by typing ls it lists all of the files and folders in my current directory, macbook.

To clear your screen, type clear in your terminal and hit enter. Everything in your terminal will be erased and you can have more space for the new sections.

If you want to check again what inside this directory, you also can view the lists by GUI as well, like usual.

cd

Another most used command is cd, which is short for ‘change directory’. This command will change your current directory to the directory which you specify on your workspace or your OS.

Now I want to change my directory from macbook to Desktop, simply I type this command (make sure the sub-directory you want to access is in the parent directory, you can do that by GUI view or typing ls command):

The . (one dot) is shorthand for the current directory. The .. (2 dots) will bring you back to the parent directory with cd commands:

When first I typed cd ./ the current directory doesn’t change at all because it will return the current directory, you can omit the / (the slash) if you want. And then by typing cd..it brings you back to the parent directory of the current directory you are in. As can be observed, the parent directory of my js directory here is Desktop.

NOTE: In case you have multiple parent directory which nested to each other, the cd.. will bring you back to the closest directory that contains the current directory, for example:

From macbook directory, I change my current directory to Desktop then I get into its child directory, js. Then I type cd.., as mentioned above, my curreny directory now is Desktop, not macbook.

So in case you want to come back to your home directory, in my case is my macbook directory, simply type cd:

pwd

pwd stands for ‘print working directory‘. When typing pwd in your terminal and hit enter, it will tell you exactly where you are:

The pwd command tells me I’m in my js directory and its path is Users/macbook/Desktop/js.

mkdir

Next, we examine another useful command, mkdir, which is short for make directory. Explicitly, this command will create a new sub-directory in your current directory you are working on. For example:

In my extra directory, there is an initial file named text.txt as can be observed by typing ls command. Then, I create a new sub-directory inside extra directory (the directory I am currently in) called sub-directory.

TIP:

Typing the short filename in your terminal is acceptable. But in case there is a name that really long or simply you don’t want to type it all, there is a quicker way. By typing some first characters to match the search and press tab (the key above the caps lock), the directory or a file you need will show up:

rmdir

rmdir is short for remove directory, this command will allow you to remove a specified directory:

In my extra2 folder, I remove 2 directories named sub1 and sub2 by using rmdir command and followed by the directories you want to remove sequentially separated by a space.

touch

This command above mkdir helps you create a new directory inside your current directory, but what about creating a new file. Here the touch the command will allow you to create a new file inside the directory you are working on:

Initially, I have a folder named sub-directory and 2 other files text.txt and text1.txt. After using touch command to create another file named text2.txt, the result is showed by typing ls.

cp

cp <source> <destination> which shorts for “copy“, this command will help you create a duplicate file you specify as source, which will be saved in destination.

But in case you want to copy the entire directory to the destination (another directory), you have to slightly modify your cp command because when you have files and directories inside of the directory you are working with, this command merely has no idea what file or directory should be copied. So everything we need is to add a flag (simply think that it followed by the main command for the extra conditions) cp -r, hereby r stands for recursive:

What I’ve done is copy all the things inside my extra folder and as a result, extra69 has exactly the same things.

rm

rm is short for remove, this command will delete the file that you specify:

If you want to remove entire a folder instead, you need to use -r flag as we did with cp:

mv

mv <source> <destination>, this command comes in handy allowing you rename a file <source> from the <destination> file:

Facebook Comments

Previous Article
Next Article

Leave a Reply

avatar
  Subscribe  
Notify of

Sign up for newsletter

* indicates required

Categories

Archives