What is Linux CLI?
The Linux Command-Line Interface (CLI) is a way to interact with the Linux operating system using text commands typed into a special window called a terminal. It’s like talking to the computer by typing instead of clicking buttons with a mouse. The CLI is used by people who need to do more complex things with their computer, like developers and system administrators. It can be used to do many different tasks, such as managing files and folders, controlling programs running on the computer, setting up internet connections, and installing and managing software. While it might look intimidating at first, the CLI is an important tool for anyone who wants to learn how to use Linux.
Benefits of learning the Linux CLI
Learning the Linux Command-Line Interface (CLI) can be very beneficial for several reasons. Here are a few of the main benefits:
- Efficiency: Using the CLI can be faster than using the graphical user interface (GUI) because you can execute tasks quickly with text commands.
- Flexibility: The CLI provides more control over the operating system and allows you to perform tasks that are not available in the GUI.
- Automation: The CLI allows you to create scripts that can automate repetitive tasks, which can save you a lot of time.
- Remote access: The CLI can be used to access and control a Linux system remotely, which is useful for managing servers or other remote systems.
- Career opportunities: Many IT-related jobs require knowledge of the CLI, so learning it can open up new career opportunities.
Overall, learning the Linux CLI can be very beneficial and provide you with new skills that can help you be more efficient and effective in managing your computer system or advancing your career.
Brief overview of the guide
This guide is for people who are new to using the Linux Command-Line Interface (CLI), which is a way to talk to your computer by typing in text commands. We’ll go through the basics of using the CLI, such as how to open the terminal, navigate the files and folders on your computer, and create, copy, move, and delete files and folders. We’ll also cover how to manage user accounts and permissions, set up internet connections, and install and manage software. The guide is written in simple language and step-by-step instructions, so it’s easy to follow along even if you’ve never used the CLI before. At the end of the guide, we’ll summarize what we covered and give some extra tips to help you become comfortable using the Linux CLI.
Getting started
Understanding the Terminal
The terminal is a window on your computer that allows you to interact with the Linux Command-Line Interface (CLI) by typing in text commands. It’s like a command center for your computer where you can control it using text commands instead of clicking on icons or buttons.
For example, let’s say you want to navigate to a folder on your computer using the terminal. You would start by typing “cd” (which stands for “change directory”) followed by the path to the folder you want to navigate to. For instance, if you want to navigate to a folder named “Documents” that is located in your home folder, you would type “cd ~/Documents” and press enter.
Once you’re in the “Documents” folder, you can create a new text file by typing “touch newfile.txt” and pressing enter. This will create a new text file named “newfile.txt” in the current folder.
You can also use the terminal to manage processes running on your computer. For example, you can use the “ps” command to see a list of all the processes currently running on your system. You can then use the “kill” command to stop a specific process by specifying its process ID.
While the terminal may seem intimidating at first, it’s a powerful tool that can help you get things done more quickly and efficiently on your Linux system. With practice, you can become comfortable with using the terminal and discover many more things you can do with it.
Opening the Terminal
To open the terminal in Linux, you can use one of the following methods:
- Using the keyboard shortcut: You can press “Ctrl + Alt + T” on your keyboard to open the terminal. This is the default keyboard shortcut in many Linux distributions.
- Using the launcher: You can also use the launcher or application menu to find and open the terminal. Simply search for “terminal” in the launcher or application menu, and click on the terminal icon to open it.
- Using the right-click menu: If you’re already in a folder in the file manager, you can right-click on an empty area and select “Open in Terminal” from the menu. This will open the terminal in the current folder.
Once you open the terminal, you’ll see a command prompt where you can start typing in commands. It’s important to note that the exact method of opening the terminal may vary slightly depending on your Linux distribution and the desktop environment you’re using.
Basic command structure
The basic command structure in the Linux Command-Line Interface (CLI) consists of three parts:
- The command itself: This is the action that you want to perform, such as “cd” to change the current directory, or “ls” to list the files in the current directory.
- The options: These are additional parameters that modify the behaviour of the command. Options are usually specified using a single hyphen (-) or double hyphen (–) followed by a letter or word, such as “-l” to show a detailed listing of files or “–help” to display the help documentation for a command.
- The arguments: These are the parameters that specify the target of the command, such as a filename or directory path. Arguments are usually specified after the command and options.
Here’s an example of a command with all three parts: “ls -l /home/user/Documents”. This command uses the “ls” command to list the files in the “/home/user/Documents” directory, and the “-l” option to display a detailed listing of the files.
It’s important to note that not all commands require options or arguments, and some commands may have different syntax or parameters depending on the version of Linux you’re using. However, most commands in the Linux CLI follow this basic structure.
Navigating the Filesystem
Understanding the Linux Filesystem
The Linux Filesystem is the structure that organizes and manages the files and directories on a Linux system. It’s different from the Windows or Mac Filesystems, and it follows a hierarchical tree-like structure.
At the root of the filesystem is the “/” directory, which is the top-level directory and contains all other directories and files. The “/” directory is also known as the “root directory”. All directories and files on a Linux system are located somewhere within the tree structure that branches out from the root directory.
Here are some of the most common directories you’ll find in the Linux Filesystem:
- /bin: This directory contains essential command-line utilities and tools that are required to run the system, such as ls, cd, and cp.
- /etc: This directory contains system configuration files, such as the network configuration files, user account information, and system-wide settings.
- /home: This directory contains user home directories, where each user has their own subdirectory to store their personal files.
- /usr: This directory contains most of the programs and supporting files used by regular users, including libraries, documentation, and shared data.
- /var: This directory contains variable data files, such as log files and temporary files.
Understanding the Linux Filesystem is important because it allows you to navigate and manage your files and directories more efficiently. When working with the Linux CLI, you’ll need to use commands to navigate the filesystem and interact with files and directories. By knowing the structure of the filesystem, you can use these commands more effectively and get things done more quickly.
Basic Navigation Commands
Here are some basic navigation commands you can use in the Linux CLI to navigate the filesystem:
- cd: This command allows you to change the current directory. For example, “cd /home/user/Documents” would change the current directory to “Documents” inside the “user” directory.
- pwd: This command displays the current working directory, which is the directory you’re currently in.
- ls: This command lists the contents of the current directory. For example, “ls” would display a list of all the files and directories in the current directory.
- mkdir: This command allows you to create a new directory. For example, “mkdir mydirectory” would create a new directory called “mydirectory” in the current directory.
- rmdir: This command allows you to remove an empty directory. For example, “rmdir mydirectory” would remove the directory called “mydirectory” in the current directory, but only if it’s empty.
- rm: This command allows you to remove files and directories. For example, “rm myfile.txt” would remove the file called “myfile.txt” in the current directory, while “rm -r mydirectory” would remove the directory called “mydirectory” and all its contents recursively.
- cp: This command allows you to copy files and directories. For example, “cp myfile.txt mycopy.txt” would create a copy of “myfile.txt” called “mycopy.txt” in the current directory.
- mv: This command allows you to move or rename files and directories. For example, “mv myfile.txt newfile.txt” would rename “myfile.txt” to “newfile.txt” in the current directory, while “mv myfile.txt /home/user/Documents” would move “myfile.txt” to the “Documents” directory inside the “user” directory.
By using these basic navigation commands, you can navigate the filesystem, create and manage directories and files, and perform various file operations. It’s important to note that these commands have many options and variations, so be sure to check the documentation or help files for more information.
Working with Files and Directories
There are many other commands you can use to work with files and directories in the Linux CLI.
- touch: This command allows you to create a new empty file. For example, “touch myfile.txt” would create a new empty file called “myfile.txt” in the current directory.
- cat: This command allows you to view the contents of a file. For example, “cat myfile.txt” would display the contents of “myfile.txt” in the terminal.
- less: This command allows you to view the contents of a file in a more user-friendly way. It allows you to scroll through the file and search for specific text. For example, “less myfile.txt” would display the contents of “myfile.txt” in a more user-friendly way.
- head: This command allows you to view the first few lines of a file. For example, “head myfile.txt” would display the first few lines of “myfile.txt”.
- tail: This command allows you to view the last few lines of a file. For example, “tail myfile.txt” would display the last few lines of “myfile.txt”.
- cp: This command allows you to copy files and directories. For example, “cp myfile.txt mycopy.txt” would create a copy of “myfile.txt” called “mycopy.txt” in the current directory.
- mv: This command allows you to move or rename files and directories. For example, “mv myfile.txt newfile.txt” would rename “myfile.txt” to “newfile.txt” in the current directory, while “mv myfile.txt /home/user/Documents” would move “myfile.txt” to the “Documents” directory inside the “user” directory.
- rm: This command allows you to remove files and directories. For example, “rm myfile.txt” would remove the file called “myfile.txt” in the current directory, while “rm -r mydirectory” would remove the directory called “mydirectory” and all its contents recursively.
- mkdir: This command allows you to create a new directory. For example, “mkdir mydirectory” would create a new directory called “mydirectory” in the current directory.
- rmdir: This command allows you to remove an empty directory. For example, “rmdir mydirectory” would remove the directory called “mydirectory” in the current directory, but only if it’s empty.
Manipulating Files and Directories
Creating and Deleting Files and Directories
Creating and deleting files and directories is an important part of working with the Linux CLI. Here are some commands that you can use to create and delete files and directories:
Creating a file: You can create a new file using the touch For example, to create a file called “myfile.txt”, you can run the following command:
touch myfile.txt
This will create an empty file called “myfile.txt” in the current directory.
Creating a directory: You can create a new directory using the mkdir command. For example, to create a directory called “mydir”, you can run the following command:
mkdir mydir
This will create a new directory called “mydir” in the current directory.
Deleting a file: You can delete a file using the rm command. For example, to delete a file called “myfile.txt”, you can run the following command:
rm myfile.txt
This will permanently delete the file “myfile.txt” from the current directory.
Deleting a directory: You can delete a directory using the rmdir command. However, this command can only be used to delete an empty directory. If you want to delete a directory and all its contents, you can use the rm command with the -r option. For example, to delete a directory called “mydir” and all its contents, you can run the following command:
rm -r mydir
This will permanently delete the directory “mydir” and all its contents from the current directory.
It’s important to be careful when using the rm command, as it can permanently delete files and directories without any confirmation. Make sure to double-check the names and paths of the files and directories you want to delete before running the command.
Copying and Moving Files
Copying and moving files are two important tasks that you may need to perform when working with the Linux CLI. Here are some commands that you can use to copy and move files:
Copying a file: You can copy a file from one location to another using the cp command. For example, to copy a file called “myfile.txt” from the current directory to a directory called “mydir”, you can run the following command:
cp myfile.txt mydir/
This will create a copy of “myfile.txt” in the “mydir” directory.
If you want to keep the same file name for the copied file, you can specify the destination path as follows:
cp myfile.txt mydir/myfile.txt
Moving a file: You can move a file from one location to another using the mv command. For example, to move a file called “myfile.txt” from the current directory to a directory called “mydir”, you can run the following command:
mv myfile.txt mydir/
This will move the file “myfile.txt” to the “mydir” directory.
If you want to rename the file while moving it, you can specify the new file name as follows:
mv myfile.txt mydir/newfile.txt
This will move the file “myfile.txt” to the “mydir” directory and rename it to “newfile.txt”.
It’s important to note that when you move a file, it will no longer be in its original location. If you want to keep a copy of the file in the original location, you should make a copy of the file before moving it using the cp command.
Renaming Files and Directories
Renaming files and directories is a common task that you may need to perform when working with the Linux CLI. Here are some commands that you can use to rename files and directories:
Renaming a file: You can rename a file using the mv command. For example, to rename a file called “oldfile.txt” to “newfile.txt”, you can run the following command:
mv oldfile.txt newfile.txt
This will rename the file “oldfile.txt” to “newfile.txt” in the current directory.
Renaming a directory: You can rename a directory using the mv command as well. For example, to rename a directory called “olddir” to “newdir”, you can run the following command:
mv olddir newdir
This will rename the directory “olddir” to “newdir” in the current directory.
It’s important to note that when you rename a file or directory, its contents and permissions remain the same. Also, be careful when renaming files or directories, as the mv command can overwrite existing files or directories if the new name is already in use.
Managing User Accounts and Permissions
User Accounts
In Linux, user accounts are used to identify and authenticate individual users who access the system. Each user account has a unique username and password, and it can be associated with one or more groups, which determine the user’s permissions and access to system resources.
Here are some common commands used for managing user accounts in Linux:
- Creating a new user account: You can create a new user account using the useradd command. For example, to create a new user called “jdoe”, you can run the following command:
useradd jdoe
This will create a new user account with the default settings.
- Setting a password for a user account: You can set a password for a user account using the passwd command. For example, to set a password for the “jdoe” account, you can run the following command:
passwd jdoe
This will prompt you to enter and confirm the new password for the user account.
- Adding a user to a group: You can add a user to a group using the usermod command. For example, to add the user “jdoe” to the “admins” group, you can run the following command:
usermod -aG admins jdoe
This will add the user “jdoe” to the “admins” group, which may have different permissions than the user’s primary group.
- Deleting a user account: You can delete a user account using the userdel command. For example, to delete the user “jdoe” and all its files, you can run the following command:
userdel -r jdoe
This will delete the user account “jdoe” and all its files and directories.
It’s important to note that managing user accounts and groups requires administrative privileges, so you may need to use the sudo command or log in as the root user to perform these tasks.
Understanding Permissions
In Linux, permissions are used to control access to files and directories. Every file and directory on a Linux system has three sets of permissions: one for the owner of the file, one for the group that the file belongs to, and one for all other users.
There are three basic types of permissions:
- Read (r): Allows a user to read the contents of a file, or list the contents of a directory.
- Write (w): Allows a user to modify the contents of a file, or create and delete files in a directory.
- Execute (x): Allows a user to execute a file, or access files and directories within a directory.
The permissions are represented by a series of nine characters, divided into three groups of three:
rwxrwxrwx
The first three characters represent the permissions for the owner of the file, the second three characters represent the permissions for the group, and the last three characters represent the permissions for all other users.
Each character can be either a letter (r, w, or x) or a hyphen (-), which indicates that the permission is not granted. For example, the following permission string:
-rw-r--r--
indicates that the owner has read and write permissions, the group has read permission, and all other users have read permission.
Here are some common commands used for managing permissions in Linux:
- Changing permissions: You can change the permissions of a file or directory using the chmod command. For example, to give the owner of a file read, write, and execute permissions, you can run the following command:
chmod u+rwx filename
This will add read, write, and execute permissions for the file’s owner.
- Changing ownership: You can change the owner and group of a file or directory using the chown and chgrp commands. For example, to change the owner of a file called “filename” to the user “jdoe”, you can run the following command:
chown jdoe filename
This will change the owner of the file to “jdoe”.
- Viewing permissions: You can view the permissions of a file or directory using the ls command. For example, to list the files in the current directory along with their permissions, you can run the following command:
ls -1
This will show a detailed listing of all files in the current directory, including their permissions.
It’s important to note that managing permissions requires administrative privileges, so you may need to use the sudo command or log in as the root user to perform these tasks. Additionally, changing permissions or ownership of system files or directories can cause serious problems, so you should be careful when performing these tasks.
Changing Permissions
In Linux, you can change the permissions of a file or directory using the chmod command. The chmod command modifies the read, write, and execute permissions for the owner, group, and other users.
The basic syntax for the chmod command is as follows:
chmod [options] mode filename
The mode parameter specifies the new permissions for the file or directory, and can be specified in either symbolic or numeric form.
In symbolic mode, you use letters to specify the new permissions. The following letters are used:
- u – the file or directory owner
- g – the group that owns the file or directory
- o – all other users
- a – all users (same as ugo)
The following letters are used to specify the new permissions:
- r – read permission
- w – write permission
- x – execute permission
For example, to add write permission for the file owner, you can run the following command:
chmod u+w filename
In numeric mode, you use a three-digit number to specify the new permissions. Each digit represents the permissions for the owner, group, and other users, respectively.
The following values are used to specify the permissions:
- 4 – read permission
- 2 – write permission
- 1 – execute permission
To set read, write, and execute permissions for the file owner, you would use the following command:
chmod 700 filename
Here, the first digit (7) specifies the permissions for the owner (read, write, and execute), while the remaining digits (0) specify that the group and other users have no permissions.
It’s important to note that changing permissions can affect the security and functionality of your system, so you should be careful when using the chmod command. Additionally, changing permissions on system files or directories can cause serious problems, so you may need administrative privileges to perform this task.
Networking
Network Commands
In Linux, there are several network commands that you can use to manage and troubleshoot network connections. Here are some of the most commonly used network commands:
- ping: The ping command is used to test the connectivity between two network devices. It sends a small packet of data to the target device and waits for a response. If the target device responds, it means that the connection is working properly.
Example usage: ping google.com
- traceroute: The traceroute command is used to trace the path that packets take from your computer to a remote device. It displays a list of all the routers and other devices that the packets pass through on their way to the destination.
Example usage: traceroute google.com
- ifconfig: The ifconfig command is used to view and configure network interfaces on your computer. It displays information such as IP address, subnet mask, and network status.
Example usage: ifconfig eth0
- netstat: The netstat command is used to display information about active network connections, network protocols, and network interfaces.
Example usage: netstat -a
- dig: The dig command is used to perform DNS lookups and retrieve information about DNS records. It can be used to check whether a domain name is resolving correctly.
Example usage: dig google.com
- wget: The wget command is used to download files from the internet. It can be used to download files over HTTP, HTTPS, and FTP protocols.
Example usage: wget https://example.com/file.zip
These are just a few of the many network commands available in Linux. Learning how to use these commands can help you troubleshoot network issues and manage your network connections more effectively.
Checking Network Status
In Linux, there are several commands you can use to check the status of your network connection. Here are some of the most commonly used commands:
- ifconfig: The ifconfig command can be used to display information about network interfaces on your system, including IP address, netmask, and status.
Example usage: ifconfig
- ip: The ip command is similar to ifconfig, but provides more detailed information about network interfaces and routing tables.
Example usage: ip addr show
- ping: The ping command can be used to test the connectivity between your computer and another network device.
Example usage: ping google.com
- traceroute: The traceroute command can be used to trace the route that packets take from your computer to a remote device.
Example usage: traceroute google.com
- netstat: The netstat command can be used to display information about active network connections, network protocols, and network interfaces.
Example usage: netstat -a
- ss: The ss command is similar to netstat, but provides more detailed information about network connections and listening ports.
Example usage: ss -tunap
These commands can help you diagnose network issues and monitor your network connection. By using them regularly, you can stay on top of your network status and quickly identify and resolve any problems that arise.
Configuring Network Settings
In Linux, you can configure network settings using several tools and commands. Here are some of the most commonly used methods:
- ifconfig: The ifconfig command can be used to configure network interfaces on your system, including IP address, netmask, and gateway.
Example usage: sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0
- ip: The ip command can be used to configure network interfaces and routing tables on your system.
Example usage: sudo ip addr add 192.168.1.100/24 dev eth0
- nmcli: The nmcli command is a command-line tool for controlling the NetworkManager daemon. It can be used to configure network settings, including IP address, netmask, gateway, DNS servers, and more.
Example usage: sudo nmcli con add con-name “MyConnection” ifname eth0 type ethernet ip4 192.168.1.100/24 gw4 192.168.1.1
- systemctl: The systemctl command can be used to manage network services, including the network service that manages network interfaces.
Example usage: sudo systemctl restart network These are just a few of the many tools and commands available for configuring network settings in Linux. By mastering these tools and commands, you can ensure that your network is configured correctly and working as expected.
Package Management
Package Managers
In Linux, a package manager is a tool that helps you install, remove, and manage software packages on your system. Here are some of the most commonly used package managers:
apt
: Theapt
command is a package manager used in Debian-based Linux distributions, including Ubuntu and Linux Mint.
Example usage:
sudo apt install firefox
yum
: Theyum
command is a package manager used in Red Hat-based Linux distributions, including CentOS and Fedora.
Example usage:
sudo yum install firefox
pacman
: Thepacman
command is a package manager used in Arch-based Linux distributions, including Arch Linux and Manjaro.
Example usage:
sudo pacman -S firefox
dnf
: Thednf
command is a package manager used in Fedora-based Linux distributions, including Fedora and CentOS/RHEL 8.
Example usage:
sudo dnf install firefox
These package managers allow you to easily install, remove, and manage software packages on your system, including updates and dependencies. By using them, you can keep your system up-to-date and secure, and ensure that you have access to the software you need to get your work done.
Installing and Updating Packages
In Linux, you can install and update software packages using package managers. Here are some of the most commonly used commands for installing and updating packages:
apt
: Theapt
command is a package manager used in Debian-based Linux distributions, including Ubuntu and Linux Mint.
To update the package list, run
sudo apt update
To install a package, run
sudo apt install package-name
To update all installed packages, run
sudo apt upgrade
yum
: Theyum
command is a package manager used in Red Hat-based Linux distributions, including CentOS and Fedora.
To update the package list, run
sudo yum update
To install a package, run
sudo yum install package-name
To update all installed packages, run
sudo yum update
pacman
: Thepacman
command is a package manager used in Arch-based Linux distributions, including Arch Linux and Manjaro.
To update the package list, run
sudo pacman -Sy
To install a package, run
sudo pacman -S package-name
To update all installed packages, run
sudo pacman -Syu
dnf
: Thednf
command is a package manager used in Fedora-based Linux distributions, including Fedora and CentOS/RHEL 8.
To update the package list, run
sudo dnf update
To install a package, run
sudo dnf install package-name
To update all installed packages, run
sudo dnf update
By using these package managers, you can easily install and update software packages on your system, ensuring that you have access to the latest features and security updates.
Removing Packages
In Linux, you can remove packages that you no longer need using package managers. Here are some of the most commonly used commands for removing packages:
apt
: Theapt
command is a package manager used in Debian-based Linux distributions, including Ubuntu and Linux Mint.
To remove a package, run
sudo apt remove package-name
To remove a package and its configuration files, run
sudo apt purge package-name
yum
: Theyum
command is a package manager used in Red Hat-based Linux distributions, including CentOS and Fedora.
To remove a package, run
sudo yum remove package-name
To remove a package and its dependencies, run
sudo yum autoremove package-name
pacman
: Thepacman
command is a package manager used in Arch-based Linux distributions, including Arch Linux and Manjaro.
To remove a package, run
sudo pacman -R package-name
To remove a package and its dependencies, run
sudo pacman -Rs package-name
dnf
: Thednf
command is a package manager used in Fedora-based Linux distributions, including Fedora and CentOS/RHEL 8.
To remove a package, run
sudo dnf remove package-name
To remove a package and its dependencies, run
sudo dnf autoremove package-name
By using these package managers, you can easily remove packages that you no longer need, freeing up space on your system and ensuring that it remains organized and efficient.
Conclusion
Summary
The beginner’s guide to the Linux command-line interface (CLI) covers the basics of using the Terminal, navigating the Linux filesystem, and working with files and directories. It also explains user accounts, permissions, and network commands. Additionally, the guide explains package managers, which are used to install, update, and remove software packages on Linux.
Some of the key topics covered in the guide include:
- Understanding the Terminal and basic command structure
- Navigation commands for moving around the Linux filesystem
- Working with files and directories, including creating, deleting, copying, moving, and renaming them
- User accounts and permissions, including how to change permissions on files and directories
- Network commands, including how to check network status and configure network settings
- Package managers, including how to install, update, and remove software packages using popular package managers like
apt
,yum
,pacman
, anddnf
.
By learning these fundamental concepts, users can become more comfortable working in the Linux command line and perform a wide variety of tasks efficiently and effectively.
Final tips and advice for mastering the Linux CLI
Here are some final tips and advice for mastering the Linux command-line interface (CLI):
- Practice, practice, practice: The more you use the Linux CLI, the more comfortable you will become with it. Try to use the command line for as many tasks as possible to build your confidence and proficiency.
- Use online resources: There are numerous online resources available for learning the Linux CLI, including tutorials, forums, and documentation. Take advantage of these resources to deepen your understanding of the CLI.
- Use tab completion: Tab completion is a feature that allows you to type the first few characters of a command or filename and then press the Tab key to automatically complete it. This can save you a lot of time and typing.
- Use the manual: Most Linux commands come with a built-in manual that you can access by typing
man command-name
. The manual provides detailed information on how to use the command and its various options and arguments. - Be careful with root access: Root access gives you complete control over the system, but it also comes with great responsibility. Make sure to use
sudo
andsu
commands with care and avoid making changes to critical system files unless you know what you are doing. - Keep your system up to date: Keeping your system up to date ensures that you have the latest security patches and bug fixes. Use your package manager to regularly update your system and installed packages.
By following these tips and advice, you can become proficient in using the Linux command-line interface and enjoy the benefits of greater control, efficiency, and flexibility in your computing tasks.