Basic Linux Commands for the Beginners

What is a Command?

A Linux command is a set of instructions or operations that can be executed in the command-line interface (CLI) of a Linux operating system. Linux commands are used to perform various tasks on a Linux system, such as managing files and directories, managing system processes, configuring the system, and much more. Linux commands are typed into the terminal and can be executed by pressing the enter key. There are hundreds of Linux commands available, and each command has its own set of options and arguments that can be used to customize its behavior.

What is Command Line Interface (CLI)?

The Command-Line Interface (CLI), also known as the command-line shell, is a means of interacting with a computer’s operating system by typing commands into a terminal or console window. The CLI provides users with a text-based interface that allows them to navigate the file system, run programs, and perform various other tasks by entering commands into the terminal. Unlike graphical user interfaces (GUIs), which use a mouse and visual elements to interact with the system, the CLI relies solely on text-based commands, making it a powerful and flexible tool for performing advanced system administration tasks and automation.

1. “ls” Command (List Files and Directories)

The ls command is used to list the files in a directory. By default, ls will show you the files in the current directory, but you can specify a different directory by including its path as an argument.

Here are a few examples of how to use the ls command:

Show the files in the current directory
ls
Show the files in the /etc directory
ls /etc
Show the files in the current directory, including hidden files
ls -a
Show the files in the current directory, including hidden files and details about each file
ls -al

2. “cd” Command (Change Directory)

The cd command is used to change the current working directory. When you open a terminal window, you start in your home directory. The cd command lets you navigate to other directories on your file system.

Here are a few examples of how to use the cd command:

Change to your home directory
cd ~
Change to the /etc directory
cd /etc
Change to the parent directory of the current directory
cd ..

3. `pwd` Command (Print Current Working Directory)

The pwd command is used to print the current working directory. This command is useful for determining your current location on the file system.

Here’s an example of how to use the pwd command:

Print the current working directory
pwd
Show the physical path of the directory in case of a symbolic link:
pwd -P

4. `cat` Command (Read and Print File Content)

The cat command is used to concatenate and display the contents of one or more files. This command is useful for quickly viewing the contents of a file.

Here are a few examples of how to use the cat command:

Show the contents of a file
cat file.txt
Concatenate and display the contents of two files
cat file1.txt file2.txt
Display the contents of a file, one page at a time
cat file.txt | less

5. `grep` Command (Pattern Searching)

The grep command is used to search for a pattern in one or more files. This command is useful for finding specific information in large files.

Here are a few examples of how to use the grep command:

Search for a pattern in a file
grep pattern file.txt
Search for a pattern in multiple files
grep pattern file1.txt file2.txt
Search for a pattern in a file, and show the line numbers of the matches
grep -n pattern file.txt
Search for a pattern in a file, and show only the matches, not the entire lines
grep -o pattern file.txt

6. `tail` Command (Print last lines from content)

The tail command is used to display the last few lines of a file. This command is useful for monitoring log files and other text files.

Here are a few examples of how to use the tail command:

Show the last 10 lines of a file
tail file.txt
Show the last 20 lines of a file
tail -n 20 file.txt
Continuously display the end of a file as it grows
tail -f file.txt

7. `head` Command (Print start lines from content)

The head command is used to display the first few lines of a file. This command is useful for quickly reviewing the contents of a file.

Here are a few examples of how to use the `head` command:

Show the first 10 lines of a file
head file.txt
Show the first 20 lines of a file
head -n 20 file.txt

8. `cp` Command (Copy Files)

The cp command is used to copy files and directories. This command is useful for making backups of important files and for copying files between directories.

Here are a few examples of how to use the `cp` command:

Copy a file
cp file.txt file_copy.txt
Copy a directory and its contents
cp -R dir1 dir2
Copy a file, and preserve its permissions and timestamps
cp -p file.txt file_copy.txt

9. `mv` Command (Move or rename files)

The mv command is used to move or rename files and directories. This command is useful for reorganizing your files and directories.

Here are a few examples of how to use the `mv` command:

Move a file
mv file.txt dir1/
Rename a file
mv file.txt file_renamed.txt

10. `rm` Command (Delete files)

The rm command is used to delete files and directories. This command is useful for freeing up space on your file system and for removing unneeded files.

Here are a few examples of how to use the `rm` command:

Delete a file
rm file.txt
Delete a directory and its contents
rm -r dir1

11. `chmod` Command (Change Permissions)

The chmod command is used to change the permissions of files and directories. This command is useful for controlling who can read, write, and execute files on your system.

Here are a few examples of how to use the `chmod` command:

Give the owner of a file read and write permissions
chmod u+rw file.txt
Give everyone execute permissions on a file
chmod +x file.txt
Set the permissions of a file to 644
chmod 644 file.txt

12. `chown` Command (Change ownership)

The chown command is used to change the owner of a file or directory. This command is useful for changing the ownership of files and directories so that they match the permissions on your system.

Here are a few examples of how to use the `chown` command:

Change the owner of a file to user1
chown user1 file.txt
Change the owner and group of a file to user1 and group1
chown user1:group1 file.txt

13. `df` Command (Check free disk)

The df command is used to display information about disk space usage on your system. This command is useful for monitoring the available space on your file system.

Here are a few examples of how to use the `df` command:

Show disk space usage for all file systems
df
Show disk space usage in a human-readable format.
df -h

14. `du` Command (Check disk uses)

The du command is used to estimate the space used by a file or directory. This command is useful for finding out which files or directories are using the most space on your file system.

Here are a few examples of how to use the du command:

Show the size of a directory and its contents
du -sh dir1
Show the size of a directory and its contents, including hidden files
du -sh dir1 --exclude=".*"
Show the size of a directory, sorted by size
du -sh dir1 | sort -hr

15. `top` Command (Display running processes)

The top command is used to display information about the processes running on your system. This command is useful for monitoring the performance of your system and identifying processes that are using too much CPU or memory.

Here are a few examples of how to use the top command:

Show the top processes on your system
top
Show only processes owned by a specific user
top -u user1
Show the full command line for each process
top -c

16. `kill` Command (Terminate processes)

The kill command is used to send a signal to a process, causing it to terminate. This command is useful for ending processes that have become unresponsive or that you no longer need.

Here are a few examples of how to use the kill command:

End a process with a specific PID
kill 12345
End a process gracefully using the INT signal
kill -INT 12345
End a process immediately using the KILL signal
kill -KILL 12345

17. `ssh` Command (Remote shell access)

The ssh command is used to securely log in to a remote system. This command is useful for remotely managing servers and other systems.

Here are a few examples of how to use the ssh command:

Login to a remote system as user1
ssh [email protected]
Login to a remote system as user1 and run a command
ssh [email protected] "ls -l"
Login to a remote system using a key pair for authentication
ssh -i /path/to/private/key [email protected]

18. `scp` Command (Remote copy)

The scp command is used to securely copy files between systems. This command is useful for copying files to and from remote systems.

Here are a few examples of how to use the scp command:

Copy a file from the local system to a remote system
scp file.txt [email protected]:~/
Copy a file from a remote system to the local system
scp [email protected]:file.txt .
Copy a directory and its contents from the local system to a remote system
scp -r dir1 [email protected]:~/

19. `sudo` Command (Priviledged access)

The sudo command is used to run a command as another user, typically the root user. This command is useful for performing administrative tasks on a system.

Here are a few examples of how to use the sudo command:

Run a command as the root user
sudo command
Run a command as the root user and keep the environment variables from your current user
sudo -E command
Run a command as another user
sudo -u user2 command
Run a graphical application as the root user
sudo -i gedit
Run a command and keep the terminal open after the command completes
sudo -b command

20. `nano` Command (Command line editor)

The nano command is a text editor that is commonly used in Linux systems. This command is useful for editing configuration files and other text files on a system.

Here are a few examples of how to use the `nano` command:

Open a file for editing
nano file.txt
Press Ctrl + O to save the changes to a file
Press Ctrl + X to exit nano
Press Ctrl + W to search for text in a file

Conclusion

In conclusion, the 20 commands listed above are some of the most essential Linux commands for system administrators. It is important to become familiar with these commands and how to use them effectively, as they will help you perform many common administrative tasks on your system. Whether you are a seasoned system administrator or just starting out, these commands are a great place to start.

How to Change Default PHP Version on Ubuntu

Switch to PHP 8.2

Run the following command to disable Apache module for other PHP versions

sudo a2dismod php* 

Now, enable PHP 8.1 module in Apache server.

sudo a2enmod php8.2 
sudo systemctl restart apache2 

For Command Line Interface:

sudo update-alternatives --set php /usr/bin/php8.2 
sudo update-alternatives --set phar /usr/bin/phar8.2 
sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.2 
sudo update-alternatives --set phpize /usr/bin/phpize8.2 
sudo update-alternatives --set php-config /usr/bin/php-config8.2 

If you want a different version (eg: PHP 8.1 or PHP 8.0) to be configured as default, just replace 8.2 with the required version.

How To Install PHP on Ubuntu 22.04

Before We Begin

First, log in to Ubuntu 22.04 via the console. Then update the Apt cache and upgrade the current packages of the system using the following command:

sudo apt update && sudo apt upgrade 

Installing PHP on Ubuntu 22.04

  • Install a few dependencies required by this tutorial with the below-mentioned command:
sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https
  • Add the Ondrej PPA to your system, which contains all versions of PHP packages for Ubuntu systems.
LC_ALL=C.UTF-8 sudo add-apt-repository ppa:ondrej/php
  • Now, update the Apt package manager cache.
sudo apt update
  • The SURY repository contains PHP 8.2, 8.1, 8.0, 7.4, 7.3, 7.2, 7.1, 7.0 & PHP 5.6. As the latest stable version of PHP is 8.2, but a large number of websites still required PHP 7. You can install any of the required PHP versions on your system.

Install PHP 8.2:

sudo apt install php8.2

Install PHP 8.1:

sudo apt install php8.1

Install PHP 7.4:

sudo apt install php7.4 
  • Most PHP applications depend on various extensions to extend their features. That can also be installed using the following syntax:
sudo apt install php8.2-[extension]

Replace [extension] with the extension you want to install, if you want to add multiple extensions then include them in braces, I am going to install “php-mbstring, php-mysql, php-xml, and php-curl” by running the below-mentioned command:

sudo apt install php8.2-mysql php8.2-mbstring php8.2-xml php8.2-curl 

Check Active PHP Version

php -v 

About PHP Configuration Files

The PHP configuration files are stored under /etc/php directory with the version numbers. For example, all the configuration files related to PHP 8.2 are located below:

  1. Main PHP configuration file location:
    • PHP CLI: /etc/php/8.2/cli/php.ini
    • Apache: /etc/php/8.2/apache2/php.ini
    • PHP FPM: /etc/php/8.2/fpm/php.ini
  2. All the installed PHP modules are stored under /etc/php/8.2/mods-available directory.
  3. PHP Active modules configuration directory location:
    • PHP CLI: /etc/php/8.2/cli/conf.d/
    • Apache: /etc/php/8.2/apache2/conf.d/
    • PHP FPM: /etc/php/8.2/fpm/conf.d/

To check files for the other PHP versions, just change the PHP version number (8.2 in the above example) in the files and directory path.

Switch Default PHP Version for CLI

You can use an `update-alternatives` command to set the default PHP version. Use this tutorial to read more details about switching the PHP version for CLI and Apache.

sudo update-alternatives --config php

Output:

There are 4 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php8.1   81        auto mode
  1            /usr/bin/php5.6   56        manual mode
  2            /usr/bin/php7.4   74        manual mode
  3            /usr/bin/php8.0   80        manual mode
  4            /usr/bin/php8.1   81        manual mode
  5            /usr/bin/php8.2   82        manual mode

Press  to keep the current choice[*], or type selection number: 2

Uninstalling PHP

If any PHP version is no more required, it can be removed from the system. That will free the disk space as well as system security.

To uninstall any PHP version just type:

sudo apt remove php5.6 

Also, uninstall all the modules for that version with the following command:

sudo apt remove php5.6-*