August 23, 2019
August 23, 2019
When hearing about Linux, most people will think of a complicated operating system that is only used by programmers. A shudder runs down their spine when imagining using the OS because it involves a lot of commands!
However, that’s not true. Yes, Linux has tons of commands, but you’ll be just fine with only a fraction of those available.
In this article, you’ll learn 30 basic commands that will undoubtedly help you navigate through Linux as a newbie.
Linux is an entire family of open source Unix operating systems, that are based on the Linux Kernel. This includes all of the most popular Linux based systems like Ubuntu, Fedora, Mint, Debian, and others. More accurately, they’re called distributions or distros.
Since the first release in 1991, it continued to gain popularity due to its open-source nature. People can freely modify and redistribute it under their own name.
Although Linux can’t beat Windows in the desktop market share, its distributions are extremely popular among web hosting providers. Thanks to the stability, flexibility, and security, 36.7% of all websites are using Linux as their servers OS.
When operating an OS, you need to use a shell — an interface that gives you access to the operating system’s services.
Most of Linux distributions use a graphic user interface (GUI) as their shell, mainly to provide ease of use for their users.
That being said, it’s more recommended to use a command line interface (CLI) because it’s more powerful and effective. Tasks that require a multi-step process through GUI can be done in a matter of seconds by typing some commands into the CLI.
So if you’re considering to use Linux, learning basic command lines will go a long way.
Before we go on to the list of commands, you need to open the command line first.
Although the steps may differ depending on the distribution that you’re using, usually you can find the command line in the Utilities section.
Now that you’re all set, let’s get started, shall we?
Use the pwd command to find out the path of the current directory (folder) you’re in. The command will return an absolute (full) path, which is basically a path that starts with a forward slash (/). An example of an absolute path is /home/username.
To navigate through the Linux filesystem, use the cd command. It requires either the full path or the name of the directory, depending on the current directory you’re in.
Let’s say you’re in /home/username/Documents and want to go to Photos, a subdirectory of Documents. To do so, simply type cd Photos.
Another scenario is if you want to switch to a completely new directory, say /home/username/Movies. In this case, you have to type cd followed by the directory’s absolute path.
There are some shortcuts if you want to navigate quickly. Use cd.. (with two dots) to move one directory up, or go straight to the home folder with cd. To move to your previous directory, type cd- (with a hyphen).
On a side note, Linux’s shell is case sensitive. Hence, you have to type the name’s directory exactly as it is.
ls command is used to view the contents of a directory. By default, this command will display the contents of your current directory.
If you want to see the content of other directories, type ls and then the directory’s path. For example, enter ls /home/username/Documents to view the content of Documents.
cat is one of the most frequently used command in Linux. It is used to view the content of a file on the standard output (sdout). To run this command, type cat followed by the file’s name and its extension. For instance: cat file.txt.
Use the cp command to copy files from the present directory. For instance, the command cp scenery.jpg /home/username/Pictures would create a copy of scenery.jpg to the Pictures directory.
The primary use of the mv command is to move files, although it can also be used to rename files.
The arguments in this command are similar to the cp command. You need to type mv, the file’s name, and the destination’s directory. For example: mv file.txt /home/username/Documents.
To rename files, the syntax is mv oldname.ext newname.ext
Use mkdir command to make a new directory — like mkdir Music will create a new directory called Music.
If you need to delete a directory, use the rmdir command. However, rmdir only allows you to delete empty directories.
The rm command is used to delete directories along with the contents within them. If you only want to delete the directory — as an alternative to rmdir — use rm -r.
The touch command allows you to create blank new files through the command line. As an example, enter touch /home/username/Documents/Web.html to create an HTML file entitled Web under the Documents directory.
You can use this command to locate a file, just like the search command in Windows. What’s more, using the -i argument along with this command will make it case-insensitive, so you can search for a file even if you don’t remember its exact name.
To search for a file that contains two or more words, use an asterisk (*). For example, locate -i school*note command will search for any file that contains the word “school” and “note”, no matter if it is uppercase or lowercase.
Similar to the locate command, using find also searches for files. The difference is, you use the find command to locate files within a given directory.
As an example, find /home/ -name notes.txt command will search for a file called notes.txt within the home directory and its subdirectories.
Another command that is undoubtedly very useful for everyday use. grep lets you search through all the text through a given file.
To illustrate, grep blue notepad.txt will search for the word blue in the notepad file. Lines that contain the searched word will be displayed fully.
Short for “SuperUser Do”, this command enables you to perform tasks that require administrative or root permissions. However, it is not advisable to use this command for daily use because it might be easy for an error to occur if you did something wrong.
Use df command to get a report on the system’s disk space usage, shown in percentage and KBs. If you want to see the report in megabytes, type df -m.
If you want to check how much space a file or a directory takes, the du (Disk Usage) command is the answer. However, the disk usage summary will show disk block numbers instead of the usual size format. If you want to see it in bytes, kilobytes, and megabytes, add the -h argument to the command line.
The head command is used to view the first lines of any text file. By default, it will show the first ten lines, but you can change this number to your liking. For example, if you only want to show the first five lines, type head -n 5 filename.ext.
This one has a similar function to the head command, but instead of showing the first lines, the tail command will display the last ten lines of a text file.
Short for difference, the diff command compares the content of two files line by line. After analyzing the files, it will output the lines that do not match. Programmers often use this command when they need to make some program alterations instead of rewriting the entire source code.
The simplest form of this command is diff file1.ext file2.ext
The tar command is the most widely used command to archive multiple files into a tarball — a common Linux file format that is similar to zip format, but compression is optional.
This command is quite complex with a long list of functions such as adding new files into an existing archive, listing the content of an archive, extracting the content from an archive, and many more. Check out some practical examples to know more about other functions.
chmod is another essential command, used to change the read, write, and execute permissions of files and directories. As this command is rather complicated, you can read the full tutorial in order to execute it properly.
In Linux, all files are owned by a specific user. The chown command enables you to change or transfer the ownership of a file to the specified username. For instance, chown linuxuser2 file.ext will make linuxuser2 as the owner of the file.ext.
jobs command will display all current jobs along with their statuses. A job is basically a process that is started by the shell.
If you have an unresponsive program, you can terminate it manually by using the kill command. It will send a certain signal to the misbehaving app and instructs the app to terminate itself.
There is a total of sixty-four signals that you can use, but people usually only use two signals:
Besides knowing the signals, you also need to know the process identification number (PID) of the program you want to kill. If you don’t know the PID, simply run the command ps ux.
After knowing what signal you want to use and the PID of the program, enter the following syntax:
kill [signal option] PID.
Use the ping command to check your connectivity status to a server. For example, by simply entering ping google.com, the command will check whether you’re able to connect to Google and also measure the response time.
The Linux’s command line is super useful — you can even download files from the internet with the help of the wget command. To do so, simply type wget followed by the download link.
The uname command, short for Unix Name, will print detailed information about your Linux system like the machine name, operating system, kernel, and so on.
As a terminal equivalent to Task Manager in Windows, the top command will display a list of running processes and how much CPU each process uses. It’s very useful to monitor the system resource usage, especially knowing which process needs to be terminated because it consumes too many resources.
When you’ve been using Linux for a certain period of time, you’ll quickly notice that you can run hundreds of commands every day. As such, running history command is particularly useful if you want to review the commands you’ve entered before.
Confused about the function of certain commands? Don’t worry, you can easily learn how to use them right from Linux’s shell by using the man command. For instance, entering man tail will show the manual instruction of the tail command.
Linux commands help users to execute tasks easily and effectively. It might take a while to remember even some of the basic commands, but nothing is impossible with lots of practice.
In the end, knowing and mastering those commands above will undoubtedly be beneficial for you. Good luck!
July 30, 2019