Linux Command Line – Tips and Tricks

Today user-friendly graphical interfaces dominate our screens.  Therefore, it might seem like the command line is a relic of the past. Yet, it remains a potent tool for anyone who wants to truly harness the power of a computer. Welcome to the world of the Linux Command Line. Here, we will uncover the tips and tricks that can transform you from a casual user into a command line maestro.

Essential Linux Command Line Basics

In this chapter, we will lay the foundation for your journey into the Linux command line. To begin with, we will start with the basics to ensure you are comfortable navigating the terminal and executing commands effectively.

Accessing the Terminal

Before you can embark on your command line adventure, you need to access the terminal. If you are unsure how to do this, we got you covered in our article on “Establishing a connection to your server via SSH” about connecting to a Linux terminal. This is practically the door to the world of command line magic. 

Basic Commands Recap

Now that you have the terminal at your fingertips, let us check some fundamental commands that form the core of Linux command line interaction. These commands are the building blocks of your journey and mastering them will boost your confidence and productivity. We will cover commands like: 

Command Description 
ls Lists files and directories in the current directory. 
cd Changes the current directory. 
pwd Prints the current working directory’s path. 
mkdir Creates one or more directories. 
touch Creates empty files or updates file timestamps. 
rm Removes files and directories. 
cp Copies files and directories. 
mv Moves or renames files and directories. 

These commands might seem simple, but they perform a wide range of basic tasks on a Linux system. We will walk you through how to use them effectively and provide practical examples to reinforce your understanding. 

Navigation and File Operations

In this chapter, we will focus on navigating the file system, managing files and directories, as well as copying and moving them. Additionally, we’ll delve into understanding file permissions and ownership.
For an even deeper dive into these topics, along with a wealth of additional commands, tips, tricks, and best practices, do not forget to explore the “How to Linux Navigation and File Management” guide. It is your go-to resource for mastering file operations and getting the most out of the Linux command line. 

Navigating the Linux Filesystem

To change to your home directory: 

cd ~

To navigate to the /var/log directory: 

cd /var/log

To display the current directory: 

pwd

How to Manipulate Files and Directories in Linux: 

To create a new directory named “my_folder”: 

mkdir my_folder

To create a new empty file named “my_file.txt”: 

touch my_file.txt

To remove a file named “old_file.txt”: 

rm old_file.txt 

How to Copy and Move Files and Directories in Linux:  

To copy a file “source.txt” to a new location “destination/”: 

cp source.txt destination/ 

To move a file “file.txt” to a different directory “new_location/”: 

mv file.txt new_location/ 

To rename a file “old_name.txt” to “new_name.txt”: 

mv old_name.txt new_name.txt 

Managing File Permissions and Ownership:  

To change the permissions of a file “file.txt” to make it readable, writable, and executable by the owner: 

chmod u+rwx file.txt 

To change the owner of a file “file.txt” to a user named “new_owner”: 

chown new_owner file.txt 

To change the group of a file “file.txt” to a group named “new_group”: 

chown :new_group file.txt 

How to Work Effectively in the Linux Command Line 

Now we will explore techniques and tools that will help you work more efficiently and productively in the Linux command line environment. Consequently, these skills are essential for streamlining your workflow and becoming a proficient command line user.

Tab Completion 

Tab completion is a lifesaver when it comes to saving time and minimizing typing errors. Simply by pressing the “Tab” key, the terminal can automatically complete filenames, directory names, and even commands. For example, if you want to navigate to a directory called “my_long_and_complicated_directory_name,” you can simply type “cd my_,” press “Tab,” and the terminal will complete the name for you. If there are multiple possibilities, pressing “Tab” twice will display a list of options.  

Linux Command History and Recall

Have you ever used a command, only to need it again a few minutes later? With command history and recall, you can easily access previously executed commands. The history command shows a list of recent commands, each associated with a number. You can rerun a command by typing an exclamation mark (!) followed by the command number (e.g., !42 will rerun the 42nd command in your history). You can also search your command history by typing Ctrl + R and then entering a keyword from the command you are looking for. This feature can save you from retyping long and complex commands. 

Using Aliases in the Linux Shell 

Aliases are like custom shortcuts for your commands. You can create your own shorthand for frequently used or complex commands. For example, if you often find yourself typing ls -l to list files in long format, you can create an alias like this: 

alias ll='ls -l'

After defining this alias, typing ll in the terminal will execute the ls -l command. You can add aliases to your shell configuration file (e.g., ~/.bashrc for Bash) to make them available every time you open a terminal session. Aliases are a powerful way to tailor the command line to your preferences and save time on repetitive tasks. 

Linux Command Line Shortcuts 

Command line shortcuts are quick key combinations that help you navigate, edit, and control your terminal more efficiently. Here are a few essential shortcuts: 

Keyboard Shortcut Description 
CTRL + A Moves the cursor to the beginning of the line. 
CTRL + E Moves the cursor to the end of the line. 
CTRL + U Deletes text from the cursor to the beginning of the line. 
CTRL + K Deletes text from the cursor to the end of the line. 
CTRL + L  Clears the terminal screen. 
CTRL + C Interrupts (stops) the current command. 
CTRL + D Exits the current shell or terminal session. 
CTRL + Z Suspends the current command (resumable with the fg command). 

Pipelines and Redirections  

Now let us take a look at the essential concepts of managing input and output in the Linux command line. Understanding standard input, output, and error, combining commands with pipes (|), redirecting output to files (> and >>), and redirecting input from files (<) are fundamental skills. 

Understanding Linux Standard Input, Output, and Error

Standard Input (stdin), Standard Output (stdout), and Standard Error (stderr) are the communication channels between commands in the Linux command line. Mastering these channels results in comprehending command behavior and diagnosing issues. Here’s how they work: 

– Standard Input (stdin): This is where a command reads input from. By default, it’s your keyboard. 

– Standard Output (stdout): This is where a command sends its normal output. By default, it’s your terminal. 

– Standard Error (stderr): This is where a command sends error messages. It’s also directed to your terminal by default. 

Learning how to redirect or capture these streams can be rather useful. 

Combining Commands with Pipes (|)  

Pipes (|) allow you to combine multiple commands by taking the output of one command and using it as the input for another. This enables you to create powerful data processing pipelines. Here’s a simple example: 

command1 | command2 

In this example, the output of command1 is passed as input to command2. You can chain as many commands as needed, creating complex workflows that process data step by step. 

Redirecting Output to Files (> and >>) 

You can redirect the output of a command to a file using > (overwrite) or >> (append). This is handy for saving the results of a command or creating log files. Here’s how it works: 

To overwrite a file with command output: 

command > output.txt 

To append command output to an existing file: 

command >> output.txt 

This feature is especially useful when you want to capture the results of a long-running process or create detailed reports. 

Redirecting Input from Files (<)  

In addition to redirecting output, you can also redirect input from files using <. This allows you to feed a command with data from a file instead of typing it manually. Here’s an example: 

command < input.txt 

In this case, command reads its input from input.txt. This is handy for automating repetitive tasks and processing large datasets. 

Searching and Manipulating Text in the Linux Shell 

Let us look at powerful tools and techniques available in the Linux command line for searching and manipulating text. These skills are useful in parsing log files, extracting specific information, and performing various text-related tasks efficiently. 

Using grep for Text Search in the Linux Terminal

grep is a versatile command-line tool for searching text within files or streams. It allows you to find lines that match a specified pattern or regular expression. Here’s a basic usage example: 

grep "pattern" file.txt  

This command will search for and display lines in file.txt that contain the specified “pattern.” Additionally, grep offers a wide range of options for advanced text searching, thus making it an essential tool for text analysis and data extraction.

Linux Text Manipulation with sed and awk 

sed and awk are two powerful text processing utilities that enable you to manipulate and transform text in several ways. 

sed: Stream Editor – sed is used for text substitution, deletion, and text manipulation. It operates on a line-by-line basis and is often used in scripts for automated text editing. For example, to replace all occurrences of “old” with “new” in a file: 

 sed 's/old/new/g' file.txt 

awk: A versatile text processing tool, awk excels at processing structured data, such as CSV files. It allows you to define custom actions for each line or record in a file. For instance, to print the second field (column) of a CSV file: 

awk -F',' '{print $2}' file.csv 

Sorting and Filtering Text (sort, cut, uniq) in Linux 

Linux provides several built-in commands for sorting, cutting, and filtering text efficiently: 

sort: The sort command arranges lines in alphabetical or numerical order. For instance, to sort a text file named data.txt: 

 sort data.txt 

cut: The cut command allows you to extract specific columns from text files, making it useful for parsing structured data. To extract the first and third columns from a CSV file: 

  cut -d',' -f1,3 file.csv  

uniq: uniq is used to filter out duplicate lines from sorted text. For example, to find unique lines in a file: 

 sort file.txt | uniq

These commands, in combination with grep, sed, and awk, provide a robust toolkit for searching, processing, and manipulating text data in the Linux command line. Whether you’re a system administrator, programmer, or data analyst, mastering these text manipulation tools will significantly enhance your productivity. 

Linux System Information and Troubleshooting 

In this chapter, we will explore essential tools and techniques for gathering system information, troubleshooting common issues, and monitoring resource usage in a Linux environment. These skills are convenient for maintaining system health and resolving problems effectively. 

Checking System Information (uname, df, free) 

To gain insights into your system’s configuration and resource utilization, you can use a variety of commands: 

Command Description Example 
uname Displays basic system information such as the kernel version and system architecture. Uname -a 
df Shows disk space usage, including information about disk partitions and their available space. df -h 
free Displays memory (RAM) usage information, including total, used, and available memory. free -m 

Linux System Logs and Troubleshooting (journalctl, dmesg)

Troubleshooting system issues often involves examining logs and messages. Two key commands for this purpose are: 

journalctl: The journalctl command provides access to the systemd journal, which contains logs for various system services and events. This tool enables you to view and filter log entries, making it invaluable for diagnosing system issues. To display recent system logs: 

journalctl -xe 

dmesg: Additionally the dmesg command displays kernel ring buffer messages, which can be useful for diagnosing hardware-related problems. It specifically shows messages related to hardware detection, driver initialization, and system boot. To view kernel messages: 

dmesg | less 

Monitoring Resource Usage (htop) 

htop is an interactive and feature-rich process viewer and system monitor. Furthermore, it provides a real-time overview of system resource usage, including CPU, memory, and processes.

It looks like this: 

Linux Command Line - Tips and Tricks (htop)

To install htop use the following command: 

On Debian/Ubuntu-based systems: 

sudo apt-get install htop 

On Red Hat/CentOS-based systems: 

sudo yum install htop 

Once installed, simply run htop in your terminal: 

htop 

htop is an excellent alternative to the basic top command. In addition, it offers a more user-friendly interface and additional features for monitoring and managing processes and system resources.

Linux Command Line Security Best Practices 

When working with the Linux command line, ensuring the security of your system is crucial, whether you’re an experienced system administrator or a beginner. Understanding best practices for Linux security is vital in today’s digital world. For a deeper dive into securing your Linux server, check out our guide titled “Best Practices for Securing Remote Connections to Your VPS.” This article provides valuable insights into enhancing the safety of your Virtual Private Server (VPS) and server environment.  

User Privileges and sudo 

One important aspect of securing your Linux system is managing user privileges effectively to control who can execute what. This is often done using the sudo tool. To gain a better understanding of user accounts, superuser privileges, and how to use sudo proficiently, read our detailed article “A Practical Guide to Superuser Accounts, Sudo, & Root.”  

Besides sudo there are other tools and techniques to control permissions – especially file permissions – in the Linux universe. For a solid foundation in this area, we recommend reading our article, “Linux Permission Basics.” This resource will help you understand how file and directory permissions work in Linux, enabling you to control access to your system’s resources effectively. 

These two articles show you some of the basic security features needed when running your own Linux server: Managing who can execute what and who can access what. 

Customization and Configuration the Linux Command Line 

Do you want to be different? Then look at the art of customizing and configuring your Linux environment to suit your preferences and needs. From tweaking your shell to personalizing the terminal and managing environment variables, these skills will help you work more comfortably. 

 Configuring Your Linux Shell (.bashrc) 

Your shell configuration file, often named .bashrc for the Bash shell, not only allows you to set environment variables but also enables you to define aliases and customize the behavior of your shell. Now, let’s delve into how to customize your Bash shell:

Open your .bashrc file: 

nano ~/.bashrc 

Add an alias to simplify a common command: 

alias ll='ls -l' 

Save your changes and apply them immediately: 

source ~/.bashrc  

These customizations can make your shell more user-friendly and efficient. 

How to Customize the Linux Terminal (color schemes, fonts) 

Personalizing your terminal can enhance your overall experience. You can adjust color schemes and fonts to make it visually appealing and comfortable to use. 

Change the Linux terminal color scheme 

# To list available color schemes: 
dconf list /org/gnome/terminal/legacy/profiles:/ 
# To set a new color scheme: 
dconf write /org/gnome/terminal/legacy/profiles:/:<profile-id>/background-color "'#000000'" 
dconf write /org/gnome/terminal/legacy/profiles:/:<profile-id>/foreground-color "'#FFFFFF'"

Adjust the Linux terminal font settings

# To list available fonts: 
dconf list /org/gnome/terminal/legacy/profiles:/ 
# To set a new font: 
dconf write /org/gnome/terminal/legacy/profiles:/:<profile-id>/font "'<font-name> <font-size>'"

Experiment with different colors and fonts to find the combination that suits you best. 

Managing Environment Variables 

Environment variables are key to configuring your Linux environment. Furthermore, you can set them for your user or system-wide in files like /etc/environment. Now, let’s explore how to set a user-specific environment variable:

Open your .bashrc file: 

nano ~/.bashrc 

Add an environment variable: 

export MY_VARIABLE="my_value" 

Save your changes and apply them immediately: 

source ~/.bashrc 

Now, you can access your custom environment variable in your shell scripts and commands. 

Conclusion: Recap of Linux Command Line Key Tips and Tricks

Key Tips and Tricks Summary 
Basic Navigation Covered commands like cd, ls, and pwd for navigating the file system. 
File Operations Explored file and directory creation, manipulation, and management with commands like touch, mkdir, cp, mv, and rm. 
Permissions and Ownership Discussed controlling file permissions and ownership using chmod and chown. 
Pipelines and Redirections Mastered combining commands with pipes “|”, redirecting input and output to files (“<”, “>”, “>>”, understanding standard input (stdin), standard output (stdout) and standard error (stderr) 
Monitoring and Managing Processes Learned how to view and manage running processes with ps, top, and kill. 
System Information and Troubleshooting Checked system information with uname, df, and free, accessed logs with journalctl and dmesg, and monitored resource usage with htop. 
Security Best Practices Explored the importance of system security, user privileges (sudo), and file permissions, with reference to in-depth articles. 
Customization and Configuration Discussed customizing the shell (bashrc), terminal appearance, and managing environment variables. 
Scroll to Top