Maker.io main logo

How To Use Some of the Most Essential Linux Commands

2023-10-11 | By Maker.io Staff

The Linux bash is a tremendously powerful tool that allows you to accomplish all manner of tasks on Unix systems efficiently and quickly, regardless of whether you use a computer with a complete setup with peripherals or a remote link to a headless system. However, starting to work with the Linux terminal can seem daunting to newcomers, and overcoming the initial learning curve is steep. Therefore, this article summarizes a few essential Linux commands you should know.

How To Use Some of the Most Essential Linux Commands

Basic File and Folder Management

Managing files and folders is among the most basic tasks you can perform using any OS. Of course, you can also perform these tasks using the Linux bash, and all of these commands are typically the same across all Unix-like operating systems, with some even working on Windows.

The most basic commands include “pwd” and “ls”. Pwd is an acronym for 'print working directory' and it outputs the currently active directory to the console. Ls, on the other hand, lists all files in the current directory:

How To Use Some of the Most Essential Linux Commands This image shows the results of a pwd and ls call without additional parameters.

You can also add the so-called “-l” (lowercase L) flag to ls so that the command produces a more human-readable output with details:

How To Use Some of the Most Essential Linux Commands This image explains the columns created by ls when using the -l flag.

Next, the “tree” command lists all files and folders with their respective content as a tree:

How To Use Some of the Most Essential Linux Commands The Linux bash can generate a tree view of folders and files.

You can use the “cd” (change directory) command to change between the folders of the computer’s storage. Doing so changes the current working directory:

How To Use Some of the Most Essential Linux Commands pwd outputs the current working directory to the terminal.

As you can see in the image above, you supply the cd command with the folder path it should change to. However, you can also use the following syntax to navigate to the current folder’s parent directory:

Copy Code
cd ..
 

Next, the “mkdir” command can create new directories, and “rmdir” removes empty ones. Both instructions allow you to specify multiple new folder names to create or multiple empty folders to remove:

Copy Code
mkdir folder_1 folder_2 folder_3
rmdir folder_1 folder_2
 

Note that you must delete all contents within a folder to delete the folder itself. The “rm” command removes the specified files. In addition, you can also use so-called wildcards to let the OS know that it should remove every file that matches the wildcard. Suppose I wanted to remove the new-folder-1 in the following example:

How To Use Some of the Most Essential Linux Commands This image shows how rmdir can remove empty folders and how doing so affects the subfolder tree.

Since the folder contains files, the OS refuses to delete it using rmdir. Therefore, I have to delete all files within new-folder-1. To accomplish that, I could type the name of every single file:

Copy Code
rm ./new-folder-1/profile-picture.png
./new-folder-1/some-image-2.jpg ./new-folder-1/some-image.png
./new-folder-1/test-pattern.png
 

However, I could also instruct rm to remove all .png and .jpg files in the folder by using the asterisk wildcard in combination with the file ending:

Copy Code
rm ./new-folder-1/*.png ./new-folder-1/*.jpg
 

Note that the wildcard may appear anywhere in the file name. The following example removes all files starting with some and ending with g:

Copy Code
rm ./new-folder-1/some*g
 

Finally, the asterisk wildcard in isolation matches all files in the folder, instructing rm to delete them all:

Copy Code
rm ./new-folder-1/*
 

Lastly, there’s a shortcut for deleting non-empty folders and recursively deleting all contents within them:

Copy Code
rm -Rf non-empty-folder-name
 

Note that rm is destructive and deletes files without asking for further confirmation. In addition, there may be no way to recover deleted files.

Next, you can create new empty files using the “touch” command:

touch new-file-1.txt some-other-file.png
 

Lastly, “cp” lets you copy files, and “mv” allows you to move files to a different location. Similar to rm, both of these commands also support wildcards and let you specify multiple files to copy or move at once:

Copy Code
mv ./some-file-1 ./some-other-file /new-location
cp ./my-file ./image-*.png /new-location
 

Both commands will always use the last folder as the target of the operation and treat all other specified folders or files as the ones to copy or move. Note that you can also use the mv command to rename a file by moving it to the same folder but changing its name:

Copy Code
mv old-file-name new-file-name
 

Performing Internet and Networking-Related Tasks

Some of the most common tasks you may face when setting up a Raspberry Pi or similar Linux-based development board is getting the device online and finding its IP address or hostname in the local network to access it remotely.

Besides other more advanced purposes, “ifconfig” allows inspecting all configured networking interfaces of a Linux-based computer. Executing the command without any flags or parameters outputs a complete list of all interfaces and their details:

How To Use Some of the Most Essential Linux Commands Without additional parameters, ifconfig generates a list of all known networking interfaces.

However, as the list can get quite extensive, ifconfig also allows you to only output the details of a single interface by supplying its name after the command, for example:

Copy Code
ifconfig en0
 

Using ifconfig allows finding the device’s local IP address and lets you verify that all other settings, such as the router’s address, are correct. You can already utilize the IP address when establishing a remote connection to a device. However, the hostname may be a more appropriate way to address a device when establishing a connection. The hostname command outputs the computer’s name in the currently connected local network:

How To Use Some of the Most Essential Linux Commands You can utilize the hostname instruction to view and change the computer’s host name.

If the name is not very memorable, the “hostname” command lets you change the computer’s name in the network by using the following syntax:

Copy Code
sudo hostname new-name
 

Finally, the “ping” command can serve as a first quick test to determine whether the computer is connected to a network and can reach other computers within the local network or the Internet. The ping command can be practical for determining whether some other computer of which you know the IP address is reachable. The instruction sends a few small networking packets to the specified target:

How To Use Some of the Most Essential Linux Commands This image shows two examples of calling the ping program. In the first example, all packets were transmitted correctly. In the second example, I disconnected the WiFi adapter, and thus, no packages were transmitted.

Finding Help

There are overwhelmingly many commands you can call in most Linux-based systems, and most of those commands support various flags and parameters you can supply. Use “man” with another command's name to learn more about a Linux instruction or program. For example, type the following to learn more about the ping command:

Copy Code
man ping
 

Calling man opens an interactive user manual that you can scroll through using the arrow keys. Pressing “q” quits the manual. All of the commands discussed so far have relevant man page entries.

Summary

Linux offers a plethora of useful built-in commands and programs that you can utilize to accomplish many different tasks. This article summarizes some that benefit beginners when they take their first steps in the Linux terminal.

Pwd, ls, cd, and tree allow you to look through folders and find files. Rmdir and mkdir can create and delete empty folders. Similarly, touch and rm create and delete files. Most of these file-related commands support multi-file operations and wildcards, which can make managing files a breeze.

Ifconfig and hostname help you find the computer's IP address and hostname in your local network, and ping is a fantastic troubleshooting tool for verifying that the connection and target computer is active.

Finally, man offers an interactive manual to learn more about Linux commands, and most programs have at least a basic entry explaining what they do and what parameters they can take.

TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum