Are you completely new to the Linux Commands and Linux environment? This guide will help you easily understand the 17 important Linux commands you need to get started with the command line (a.k.a the “Terminal”) in a pretty simple way.
All the commands explained below works perfectly with both Mac and Linux Command line.
Why learn Linux Command line/Terminal?
If you don’t know what a Terminal is, it is basically a program that can do almost any task for you in a fairly fast and efficient way. All you have to do is give instructions about what you want to be done by specifying commands to it (which this guide is all about).
From a System Administrator to an Ethical Hacker, Development to Deployment you are most probably going to play around the Command line. And believe me, this skill will put superpowers in your hand.
On Ubuntu hitctrl+alt+Tand for other Linux users press alt+f2 and type gnome-terminal and hit enter. For Mac users use Spotlight(command+spacebar) and type terminal and double click the search result. Final Step: Take your mouse aside because the keyboard is your best friend from now on.
Now, you are up and running to follow this guide.
Basic Linux Commands
1.pwd (print work directory)
Easiest of them all this command lets you know in which directory you currently are (default is /home/user or ~). Stands for Print Work Directory (or Present Work Directory is what I call it). The sole purpose of this command is to show you the full path of the current directory when you feel lost.
2.ls (lists files of a directory)
The most common among other Linux commands ls (remember it by ‘lists’) shows the directories and files inside a specified directory. To view all the contents (including hidden files and directories) of a directory use 'ls -a'(-a is an attribute for ‘all’).
3.cd (changes the directory)
Short for Change Directory and quite self-explanatory. Just type‘cd directoryName (or the full path to that directory)’ and boom! you are there. cd alonewill get you to your home directory and ‘cd ..’ will let you step one directory up. Just make sure to spell the directory as it is because it is case sensitive (do not mess uppercase with lowercase letters).
Note: If you have a directory name having spaces in between (like ‘New Folder’),'cd New Folder' will get you a ‘directory does not exist’ error. What happening is cd is considering 'New' and 'Folder' as two different arguments. Instead, use a backslash (\) before spaces in between names. For example,'cd New\ Folder', 'cd my\ new\ project' etcetera.
This goes for other Linux commands below.
4.touch (makes new file)
This command creates a new file of a specified name just by 'touch fileName'.
The real purpose of touch (at least in my opinion)is to update the last modified time of an existing file (like modifying a file and it remembers when you last modified it), you should try it yourself.
5.mkdir (makes new directory)
Pretty much liketouch,mkdir(short forMakeDirectory) creates a directory of specified name by'mkdir directoryName', that’s it! There’s not much to it.
6.rm (removes files and directories)
What about deleting or removing files and directories, rm (of course short for remove) got you there use'rm removeMe' for files and empty directories and 'rm -rf directoryWithFiles' for directories with files and other directories in it.
Make sure to name the file or directory correctly (it’s very case sensitive) because as soon as you hit enter it’s gone entirely.
7.cp (copies files and directories)
Short for copy, this command copies a file or an entire directory to a specified destination, that’s why it takes two arguments (a file or directory to copy and the path to where you want to copy). For a file use 'cp fileName /path/to/the/destination' and for a directory use 'cp -r directoryName path/to/the/destination'.
8.mv (moves files and directories)
Use the mv command to move files and directories from one to another destination. Just like cp it also takes two arguments (a file or directory to move and the path to where you want to move). Example ‘mv moveMe /path/to/the/destination’.
There’s another very interesting (and common in my opinion) use case of this command which is renaming a file or directory. Use ‘mv something somethingElse’ to rename a file or directory named ‘something’ to ‘somethingElse’.
9.clear (clears the terminal)
Arguably the simplest of all the other Linux Commands. As the name suggests it clears up your cluttered terminal and turns it into a fresh one just clear it.
10.exit (exit out the terminal)
As self-explanatory as it is hitting exit will exit you out from the current terminal window. It might ask you to confirm whether you really want to leave the window (yes or no) hit Y to confirm.
So, now that you have learned the basic Linux commands to be able to survive in this dark Terminal world, but if you want to take a deeper dive into that, the following commands are here to help.
Intermediate Linux Commands
11.sudo (superuser privilege)
You might have or will be encountering a “Permission denied” or similar error quite a while in the terminal this is because some operations require superuser (or root user) privileges to execute. sudo short for Super User DO (they should have called it “please”) allows the user to perform special and advance operations (like editing a system file) and you will be asked your user password for that so remember that. Just prefix sudo to that command and it will execute without any privilege error.
Use ‘sudo !!’ to execute previous command as super user.
12.echo (text to file)
Like echoing something to a file, this command lets you quickly create a short text file by 'echo Your text goes here >> textFile.txt'.
There’s no need to escape spaces or use the backslash (\) here, the double angle brackets (>>) will take care of that.
13.cat (read text files)
So you have created a text file, now use cat (easier to remember than “concatenate”) to read it by using 'cat textFile.txt' and the resultant text will be shown in your terminal.
14.nano (a command-line text editor)
A full-fledged text editor for the terminal like notepad or notepad++ which lets you create, edit and view text and code files. There may be more text editor installed in your command line like vi, vim, emacs, etc. which can do the work too. ‘nano fileName.txt’ will open the file if it exists or create and open it if it doesn’t now, you can view and edit the file. Once done hit ctrl+x then press Y and enter to save it. Coding on nano can be painful sometimes, I recommend using vim for that.
15.zip and unzip (compressing and extracting zip files)
For starters, these commands do what they say.
You may have zipped and unzipped files before, but here’s a little trickier.
Using'zip demo.zip *' inside a directory will create a demo1.zip file containing all the files inside that directory.
If you want specific files to be zipped you can do that using 'zip demo3.zip file1.py file2.py ...' (you get the idea), this will create demo1.zip file containing the files you specified earlier.
Unzipping files is pretty simple just do 'unzip demo2.zip' to extract all files in the same direction or you can specify the directory by 'unzip demo.zip -d /path/to/directory/' and that’s really it.
16.chmod (manage permissions)
Short for CHange MODe, it helps you set permissions for files and directories. It determines who can access the files and how they access it. In this example ‘chmod 751 timeMachine.py’ the permissions are set in this way that the owner of the timeMachine.py can read, write and execute the file, member of the owner’s group can read and execute the file and other users can only execute the file. The first argument of the command takes three numbers for three different types of users (and the second argument is your file name):
The first number sets permission for the Owner.
The second number sets permission for the Owner’s group members.
The third number sets permission for other users.
An individual number can be the sum of the below numbers:
4 stands for “read”.
2 stands for “write”.
1 stands for “execute”.
0 stands for no permissions.
I hope you can now understand the example given above, try to explain the permissions ‘chmod 640 file.py’ is setting in the comments down below. chmod is much more a topic on its own than a command so, if you didn’t get it in the first attempt, it’s completely fine. I recommend you to read this page to get a deep understanding.
17.uname (provides system information)
This command provides you the information about your system by simply using uname. To get almost all the information about your system use 'uname -a'.
Now, that you’ve come this far there are some bonus terminal tricks for you that’ll definitely enhance your command-line experience:
Use the tab key for auto-completion of long paths and file names. Suppose you want to change directory to Desktop from home, ‘cd Des’ + tab will complete the command to ‘cd Desktop’.
Use the up-arrowkey to get to the previous commands to execute it as it is or modify it if you want. You can hit up-arrow key until you get back to the command you executed earlier.
The poweroff command lets you shut down your computer in style and use the reboot command to restart.
Use ctrl+c to stop an ongoing command safely (ctrl+z in some cases).
The man command (short for manual) will open up the manual for any command you learned above. For example ‘man chmod’ will open the manual for chmod command.