Skip to main content

Linux Basics: A Beginner’s Guide


Linux is a family of free and open-source operating systems based on the Linux kernel. Operating systems based on Linux are known as Linux distributions or distros. Examples include Debian, Ubuntu, Fedora, CentOS, Gentoo, Arch Linux, and many others1.

Linux is widely used in various contexts, such as web servers, cloud computing, smartphones, embedded devices, supercomputers, and more. Linux is known for its versatility, stability, security, and customizability


The Terminal and the Shell

The terminal is an input and output environment that presents a text-only window running a shell. A shell is a program that exposes the computer’s operating system to a user or program. In Linux systems, the shell presented in a terminal is a command line interpreter

The command line interface is a user interface (managed by a command line interpreter program) which processes commands to a computer program and outputs the results.

To use the terminal, you need to type commands and press Enter. The commands are usually composed of a command name followed by some options and arguments. For example, the command ls -l /home lists the files and directories in the /home directory in a long format.

The terminal is interactive: you specify commands to run and the terminal outputs the results of those commands. You can also use the up and down arrow keys to navigate through your command history and edit them.

There are many different shells available for Linux, such as bash, zsh, ksh, tcsh, and more. Each shell has its own features and syntax, but they all share some common commands and conventions. In this post, we will use the bash shell, which is the default shell in most Linux distributions.


Basic Commands for File and Directory Operations

One of the most common tasks that you will perform in the terminal is working with files and directories. Linux has a hierarchical filesystem, which means that everything is organized in a tree-like structure. The root of the tree is denoted by /, and every other file or directory is a descendant of the root.

Some of the basic commands for file and directory operations are:

  • ls: list the contents of a directory
  • cd: change the current working directory
  • pwd: print the current working directory
  • mkdir: create a new directory
  • rmdir: remove an empty directory
  • cp: copy a file or a directory
  • mv: move or rename a file or a directory
  • rm: remove a file or a directory
  • touch: create an empty file or update the timestamp of a file
  • ln: create a link to a file or a directory


These commands can take various options and arguments to modify their behavior. For example, ls -a lists all files and directories, including the hidden ones that start with a dot. cp -r copies a directory and its contents recursively. rm -f removes a file without prompting for confirmation.

You can use the man command to get more information about any command. For example, man ls shows the manual page for the ls command, which explains its usage, options, and examples.


# List the files and directories in the current working directory
$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
# Change the current working directory to the Downloads directory
$ cd Downloads

# Print the current working directory
$ pwd
/home/user/Downloads

# Create a new directory called test
$ mkdir test

# List the files and directories in the Downloads directory, including the hidden ones
$ ls -a
.  ..  test  .hidden_file
# Copy the .hidden_file to the test directory
$ cp .hidden_file test

# List the contents of the test directory
$ ls test
.hidden_file

# Move the test directory to the Desktop directory
$ mv test ~/Desktop

# Remove the .hidden_file from the Downloads directory
$ rm .hidden_file

# Create an empty file called new_file
$ touch new_file

# Create a symbolic link to the new_file called link_file
$ ln -s new_file link_file

# List the files and directories in the Downloads directory, showing the file types
$ ls -F
link_file@  new_file

Input and Output Redirection and Pipes

Linux commands can take input from different sources and send output to different destinations. By default, the input source is the keyboard and the output destination is the terminal. However, you can redirect the input and output to files or other commands using some special symbols.

Some of the symbols for input and output redirection are:

  • >: redirect the output of a command to a file, overwriting the file if it exists
  • >>: redirect the output of a command to a file, appending to the file if it exists
  • <: redirect the input of a command from a file
  • |: redirect the output of one command to the input of another command


Here are some examples of using these symbols:

# Write the current date and time to a file called date.txt
$ date > date.txt

# Append the current username to the date.txt file
$ whoami >> date.txt

# Read the contents of the date.txt file
$ cat date.txt
Thu Dec 14 09:32:17 IST 2023
user

# Count the number of words in the date.txt file
$ wc -w < date.txt
2

# List the files and directories in the current working directory and sort them alphabetically
$ ls | sort
link_file
new_file

Some Useful Commands for Text Processing and System Information

Linux has a rich set of commands for text processing and system information. These commands can be used to manipulate, filter, search, and analyze text files or the output of other commands. Some of these commands are:

  • cat: concatenate and print files
  • echo: display a line of text
  • grep: search for a pattern in a file or input
  • sed: perform text transformations
  • awk: process and generate text
  • cut: extract fields from a line of text
  • sort: sort lines of text
  • uniq: remove duplicate lines of text
  • ps: report information about processes
  • top: display dynamic information about processes
  • free: display memory usage
  • df: display disk space usage
  • du: display disk usage by files and directories
  • ping: test the network connectivity to a host
  • curl: transfer data from or to a server
  • ssh: connect to a remote server securely
  • scp: copy files securely between hosts

These commands can also take various options and arguments to modify their behavior. You can use the man command to get more information about any command.

Here are some examples of using these commands:

# Display the contents of the date.txt file
$ cat date.txt
Thu Dec 14 09:32:17 IST 2023
user

# Display the message "Hello, world!"
$ echo "Hello, world!"
Hello, world!

# Search for the word "user" in the date.txt file
$ grep user date.txt
user

# Replace the word "user" with "admin" in the date.txt file and save the output to a new file called new_date.txt
$ sed 's/user/admin/' date.txt > new_date.txt

# Display the contents of the new_date.txt file
$ cat new_date.txt
Thu Dec 14 09:32:17 IST 2023
admin

# Print the second field of each line in the new_date.txt file, using space as the field separator
$ awk '{print $2}' new_date.txt
Dec
2023

# Print the first three characters of each line in the new_date.txt file
$ cut -c 1-3 new_date.txt
Thu
adm

# Sort the lines of the new_date.txt file in reverse order
$ sort -r new_date.txt
admin
Thu Dec 14 09:32:17 IST 2023

# Remove the duplicate lines from the new_date.txt file
$ uniq new_date.txt
Thu Dec 14 09:32:17 IST 2023
admin

# Report information about the current processes
$ ps
  PID TTY          TIME CMD
  123 pts/0    00:00:00 bash
  456 pts/0    00:00:00 ps

Keep exploring, and soon you'll be navigating the Linux terminal like a pro!



Comments

Popular posts from this blog

Cockpit vs. Webmin: A Detailed Comparison for Linux Administration

  Introduction In the realm of Linux system administration, having efficient tools for managing and monitoring servers is crucial. Two popular tools that system administrators often use are  Cockpit  and  Webmin . Both of these tools provide a graphical interface accessible via a web browser, simplifying the management of Linux systems. This blog post will explore what Cockpit and Webmin are, their purposes, a comparison table, and additional information to help you choose the right tool for your needs. What is Cockpit? Overview Cockpit is a web-based graphical interface for managing Linux systems. It is designed to be easy to use, enabling both experienced and novice administrators to manage their systems effectively. Cockpit integrates seamlessly with the system’s existing infrastructure, providing real-time monitoring and management capabilities. Purpose Cockpit is primarily used for: Monitoring system performance and resource usage Managing system services Handli...

How to Set Up Custom Screen Resolution on Fedora 38 Permanently

  If you are using Fedora 38 as your operating system, you may have encountered some issues with the screen resolution. The default resolution may not be suitable for your monitor or your preferences, and you may want to change it to a higher or lower value. However, changing the resolution from the Settings menu may not work properly, or it may not persist after a reboot. In this blog post, I will show you how to set up a custom screen resolution on Fedora 38 permanently using some simple commands and configuration files. The first step is to disable the Wayland display server, which is the default display server for Fedora 38. Wayland is a modern and secure display server, but it may not support some custom resolutions or drivers. To disable Wayland, you need to edit the /etc/gdm/custom.conf file as root. You can use any text editor of your choice, such as nano, vim, or gedit. To open the file with nano, for example, you can type the following command in the terminal: sudo nano ...

Key Concepts and Tools for a Linux System Administrator

  A Linux System Administrator needs to have a comprehensive understanding of various concepts and tools to manage, configure, and maintain Linux systems effectively. Below is a categorized list of essential skills and tools with brief descriptions. Category Key Concepts & Tools Description Operating System Linux Distributions (e.g., Fedora, Ubuntu, CentOS) Knowledge of different Linux distributions, their package management systems, and unique features. Kernel Configuration and Management Understanding how to configure and optimize the Linux kernel for different workloads. System Boot Process (GRUB, systemd) Familiarity with the boot process, bootloaders, and system initialization processes. Command Line Skills Bash Shell Scripting Ability to write and debug shell scripts for automation of tasks. Core Commands (ls, cp, mv, rm, find, grep, awk, sed) Proficiency in using basic and advanced command-line utilities for system management. File System File System Hierarchy Standard (...