The command line, also known as the terminal or shell, is a text-based interface where users can type commands to perform various tasks on the Linux system. The shell interprets these commands and executes them, providing feedback in the form of text output.
There are several types of shells available in Linux, with the most common ones being bash
(Bourne Again SHell), zsh
(Z SHell), and fish
(Friendly Interactive SHell). bash
is the default shell in most Linux distributions and is the one we will primarily focus on in this guide.
A typical Linux command follows the structure: command [options] [arguments]
. The command
is the name of the operation you want to perform, options
modify the behavior of the command, and arguments
are the objects on which the command acts. For example:
ls -l /home/user
Here, ls
is the command, -l
is an option that displays detailed information, and /home/user
is the argument specifying the directory to list.
pwd
: Print the current working directory.pwd
cd
: Change the current directory.# Go to the home directory
cd ~
# Go to a specific directory
cd /var/log
# Go back to the previous directory
cd -
ls
: List the contents of a directory.# List all files and directories in the current directory
ls
# List all files and directories including hidden ones
ls -a
# List files and directories in long format
ls -l
mkdir
: Create a new directory.mkdir new_directory
rmdir
: Remove an empty directory.rmdir new_directory
rm
: Remove files or directories (use with caution).# Remove a file
rm file.txt
# Remove a non-empty directory recursively
rm -r directory
touch
: Create an empty file.touch new_file.txt
nano
, vim
, or emacs
: Text editors for creating and editing files.# Open a file in nano editor
nano new_file.txt
cp
: Copy files or directories.# Copy a file
cp source_file.txt destination_file.txt
# Copy a directory recursively
cp -r source_directory destination_directory
mv
: Move or rename files or directories.# Move a file to a different directory
mv file.txt /home/user/new_location
# Rename a file
mv old_name.txt new_name.txt
cat
: Concatenate and display the contents of a file.cat file.txt
less
: View the contents of a file page by page.less large_file.txt
head
: Display the first few lines of a file.head -n 10 file.txt
tail
: Display the last few lines of a file.tail -n 10 file.txt
grep
: Search for a pattern in a file or input.grep "pattern" file.txt
apt update
: Update the package list.sudo apt update
apt upgrade
: Upgrade installed packages.sudo apt upgrade
apt install
: Install a new package.sudo apt install package_name
apt remove
: Remove a package.sudo apt remove package_name
yum update
: Update the package list and installed packages.sudo yum update
yum install
: Install a new package.sudo yum install package_name
yum remove
: Remove a package.sudo yum remove package_name
uname
: Print system information.uname -a
hostname
: Print the system’s hostname.hostname
df
: Display disk usage information.df -h
free
: Display memory usage information.free -m
ps
: Display information about currently running processes.ps -ef
top
or htop
: Monitor system processes in real-time.top
Press the Tab
key while typing a command or a file/directory name to autocomplete. This saves time and reduces the chance of typos.
Press the Up
and Down
arrow keys to access previously executed commands. You can also use the history
command to view the entire command history.
history
|
) allow you to send the output of one command as input to another command. For example:ls -l | grep ".txt"
>
and >>
) allows you to send the output of a command to a file. >
overwrites the file, while >>
appends to it.ls > file_list.txt
Use the man
command to read the manual pages of commands. For example:
man ls
The Linux command line is a powerful tool that offers a wide range of capabilities for beginners and advanced users alike. By mastering the fundamental commands and following best practices, you can efficiently manage your system, perform complex tasks, and become more productive. Remember to practice regularly and refer to the cheat sheet when needed.