20 Basic Linux Commands

There are a gazillion commands that you can deploy on the command line for various purposes.

You can create a file, sort a file, connect to a remote server via ssh and do a million other things on the command line.

In this post, I will provide a list of basic Linux commands that I think are important for any beginner to know.

These commands are distribution agnostic, i.e. they should work on Ubuntu, Linux Mint, Red Hat, CentOS or any other Linux system.

So let’s get started with some key Linux commands.

Show Date & Time
$ date
Thurs Dec  8 14:59:10 EST 2015
System Uptime

How long has the system remained up?

That’s easy to find out.

$ uptime
13:22:14 up 31 min,  2 users,  load average: 1.00, 1.01, 0.92
Who You’re Logged in As
$ whoami
jason
History of Successful User Login

The last command provides both user login and system reboots.

$ last
jason pts/1        :0               Thu Dec 17 13:31   still logged in   
jason pts/0        :0               Thu Dec 17 13:15   still logged in   
jason :0           :0               Thu Dec 17 12:51   still logged in   
(unknown :0           :0               Thu Dec 17 12:50 - 12:51  (00:01)    
reboot   system boot  3.10.0-229.20.1. Thu Dec 17 12:50 - 13:32  (00:42)    
jason pts/2        :0               Thu Dec 17 09:53 - 10:44  (00:50)
Change Directory to Home
$ cd

Voila, with the above command you’ve moved to the home directory.

You can also use cd to move to a different directory.

For example, if you want to move to the etc directory, go with the following command.

$ cd /etc

If you’re new to Linux, before you change to a different directory it’s always good to know your current directory.

Show Current Directory
$ pwd

Knowing the files in a directory is one of the most important things for a Linux newbie to understand.

List Files
$ ls

But with the plain ls command you will not get to see the hidden files.

To see hidden files in a directory, you must use option -a.

List All Files Including Hidden Files
$ ls -a

A Linux system is running several processes at the same time.

To see the various currently active processes running on your Ubuntu or CentOS system, run the ps command.

Display Current Active Processes
$ ps

To see all running processes, the command is top.

Display All Running Processes
$ top
Kill Process Using PID No

Some processes are not desirable.

The reasons could be several. The process may be a memory hog, an undesirable visitor to your mail server etc.

If you know the process ID of the unwanted process, killing it is simple.

$ kill pid
Remove File

Linux administrators are frequently creating and removing files and directories depending on their needs.

But be careful while removing file since your Linux system may not function if you remove key system files.

$ rm file_name
Force Remove File
$ rm -rf file_name
Remove Empty Directory

Based on the type of directory, there are different commands to remove a directory.

For instance, if you’re trying to remove an empty directory, go with the rmdir command.

$ rmdir dir_name

But if you’re trying to remove a directory containing files, then the command to use is rm -r.

$ rm -r dir_name
Create a File

There are multiple techniques to create a file via the terminal.

Here are a bunch of them.

$ touch file_name
$ echo -n > file_name 
$ cat > file_name 
Then hit ctrl D
Create File with Some Content

Did you know that you can use the terminal as a rudimentary word processor?

The following examples will let you create files with some content.

You will not be able to write a dissertation with the following commands but you can certainly use them to create short files quickly without having to rely on tools like nano or vim.

$ echo jack and jill went up the hill > file_name
$ cat > file_name
jack and jill went up the hill
Then hit ctrl D
Move a File

The beauty of Linux is how easy it is to move files from one directory to another.

$ mv file1 file2
Show Memory

When you want a quick glance at memory (RAM) installed and used, go with the below free command.

$ free -m
              total        used        free      shared  buff/cache   available
Mem:           5645        1093        2134         151        2417        4060
Swap:          3855           0        3855
CPU Information

You can check the processor brand running on your Linux system, the processor family, number of cores, cache size and more with the /proc/cpuinfo command.

$ cat /proc/cpuinfo
Disk Usage

We can get disk usage in a human readable format with the below command.

$ df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   50G  9.0G   42G  18% /
devtmpfs                 2.8G     0  2.8G   0% /dev
tmpfs                    2.8G  148K  2.8G   1% /dev/shm
tmpfs                    2.8G  9.0M  2.8G   1% /run
tmpfs                    2.8G     0  2.8G   0% /sys/fs/cgroup
/dev/mapper/centos-home  179G   71G  108G  40% /home
/dev/sda1                497M  155M  343M  32% /boot

If the above commands have whetted your appetite top learn more about Linux, pick up The Linux Command Line by William Shotts, jr from your local library to expand your knowledge on how to work with the command line.

You must be logged in to post a comment Login