Linux Commands

Basic Linux commands you should know

As you transition from Windows or Mac to Linux, you will spend a lot of time working on the Linux terminal. The terminal is a console that accepts commands typed in by a user and executes a task on the system. Running commands on the terminal is an essential skill that any Linux user needs to administer efficiently.

Linux provides tons of commands, but we will keep it simple in this guide and shed light on the basic Linux commands you really ought to know as you get started.

The pwd command

At any given point on the terminal, you are on a specific directory path. To reveal the path you are working on, run the pwd command. The pwd command, short for Print Working Directory, is a basic Linux command that displays or prints out the full path of the directory you are currently in.

$ pwd

I’m currently in the /home/winnie path from the image above, which is my home directory.

The ls command

The ls command ( list ) lists the contents of a directory. In its basic form, it lists all the contents as shown.

$ ls

The -l option provides additional information such as file permissions, user and group ownership, file size (kb), date and time that the file or directory was last modified, and the file or directory name.

$ ls -l

The -h option prints out the file size output in a more user-friendly manner, as shown.

$ ls -lh

Lastly, you can list files on another directory path by specifying the path to the directory as follows:

$ ls /path/to/directory

For instance to list the contents in the /boot/grub/ path, run:

$ ls /boot/grub/

The cd command

The cd command is the shortened form for the change directory. It allows you to exit your current directory path and navigate to other directories.

To navigate a different directory, specify the full or absolute path to the directory from the root (/) directory. This is known as absolute referencing.

$ cd /path/to/directory

For instance, to navigate to the /ssh directory, run.

$ cd /etc/ssh

Here, the /etc/ssh is the absolute path.

If you are navigating to a subdirectory or a directory within your current directory, don’t start with the forward-slash ( / ). Simply specify the directory name after the cd command. This is known as relative referencing. The relative path is defined from your current working directory and not for the root directory.

$ cd directory

I’m switching to the Downloads directory in the example below, within my present working directory.

$ cd Downloads

Without any arguments, the cd command takes you back to your home directory no matter where you are on the terminal.

$ cd

The mkdir command

The mkdir command ( the shortened form for make directory ) creates a new directory within the current working directory. Simply use the syntax:

$ mkdir directory

For instance, to create a directory or folder with the name, reports, invoke the command:

$ mkdir reports

You can also create a directory within a directory using the -p option as shown.

$ mkdir -p reports/sales/2020

The command creates two directories: the sales directory and the 2020 directory inside the sales directory. To verify the directory structure, use the tree command as shown.

$ tree reports

The touch command

The touch command is used when you want to create a new file. Simply use the syntax shown to create a file.

$ touch filename

To create a simple text file called myfile.txt, issue the command:

$ touch myfile.txt

The created file inherits the user and group ownership of the user that created the file.

The rm command

An abbreviation for remove, the rm command is used for removing or deleting a file or directory. To delete or remove a file, run:

$ rm filename

For example, to remove the file we created in step 5, run the command

$ rm myfile.txt

To delete a directory, use the -R flag as shown. This deletes the directory recursively, i.e., alongside its contents.

$ rm -R directory

With that in mind, we can delete the reports directory alongside its contents as shown.

$ rm -R reports

The rmdir command

The rmdir command only deletes AN EMPTY directory. I have an empty directory called projects in my current directory. To delete it, I will execute the command:

$ rmdir projects

If you try to remove a non-empty directory, you will get the error shown below. Here, I have copied the sales.pdf file to the projects directory. Since the projects directory now contains a file, the rmdir command now fails.

cp command

The cp ( copy ) command creates a copy of a file or a directory. We can copy a file from one directory to another using the syntax shown.

$ cp /path/to/source/file /path/to/destination/directory

To copy a file called sales.pdf from your current folder to the /tmp/records/ folder on my system, I will execute the command:

$ cp sales.pdf /tmp/records/

To copy a directory recursively (including all the contents ) from one location to another, invoke the -R option. In the example below, we are copying the folder called data from the current working directory to the /tmp/records/ directory.

$ cp -R data /tmp/records/

mv command

Depending on how it is used, the mv ( move ) command can either move or rename a file/directory.

To rename a file called sales.pdf in my current directory to marketing.pdf, execute the command:

$ mv sales.pdf marketing.pdf

Renaming a file only happens when the directory is not changed. If the directory is changed, the mv command moves the file to another location. The difference between copying and moving is that copying retains the original file in its current directory but moving entirely relocates the file to a different directory

The command below moves the sales.pdf file to the /tmp/data directory.

$ mv sales.pdf /tmp/data

Notice how the file no longer exists in the current directory after being moved to a different directory.

The cat command

The cat command displays the contents of a file or shell script

$ cat

The whoami command

The whoami command displays who you are currently logged in as. In this case, I’m currently logged in as the user winnie.

$ whoami

You can also yield the same result using the who command.

$ who

The uptime command

The uptime command provides insights on how long the system has been running or active since it was powered on. Without any command options, it displays the current time, the duration it has been running in the day:hour: min format, logged-in users, and the load average.

$ uptime

In the above command, we can see that the current time is 21:43:30 hours and that the system has been up for 4 hours and 51 min, with 1 logged-in user.

To display the active time, only use the -p option.

$ uptime -p

To display the time it was powered on and started running, pass the -s option.

$ uptime -s

That’s just about it with the uptime command.

The top command

The top command provides insights about the currently running processes and a wealth of information, including the uptime statistics, CPU, and memory utilization.

$ top

The first line shows the uptime statistics, followed by total running tasks, the nature of various tasks, and CPU and memory utilization.

The free command

The free command prints statistics on main memory as well as swap usage. With the -h option, it displays the memory in a more human-readable format.

$ free -h

The df command

The df ( disk free ) command prints out the disk space utilization of all filesystems and mount points. The -Th options format the output in a more friendly and readable format.

$ df -Th

Wrapping up

As you move along, you will encounter more complex commands with more options for executing complex tasks. However, these commands provide a basic foundation to help you get started with your journey to becoming a Linux guru.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.