Linux Commands
Linux Commands
Linux Commands
A Linux command is a program or utility that runs on the CLI – a console that interacts
with the system via texts and processes. It’s like the Command Prompt application in
Windows. Linux commands are executed on Terminal by pressing Enter at the end of the
line. You can run commands to perform various tasks, from package installation to user
management and file manipulation.
A command may contain an option or a parameter. In some cases, it can still run
without them. These are the three most common parts of a command:
CommandName is the rule that you want to perform.
Option or flag modifies a command’s operation. To invoke it, use hyphens (–) or
double hyphens (—).
Parameter or argument specifies any necessary information for the command.
Keep in mind that all Linux commands are case-sensitive.
3] cd command
To navigate through the Linux files and directories, use the cd command. Depending on
your current working directory, it requires either the full path or the directory name.
Running this command without an option will take you to the home folder. Keep in mind
that only users with sudo privileges can execute it.
Let’s say you’re in /home/username/Documents and want to go to Photos, a
subdirectory of Documents. To do so, enter the following command:
cd Photos.
If you want to switch to a completely new directory, for
example, /home/username/Movies, you have to enter cd followed by the directory’s
absolute path:
cd /home/username/Movies
Here are some shortcuts to help you navigate:
cd ~[username] goes to another user’s home directory.
cd .. moves one directory up.
cd- moves to your previous directory.
4] ls command
The ls command lists files and directories within a system. Running it without a flag or
parameter will show the current working directory’s content.
To see other directories’ content, type ls followed by the desired path. For example, to
view files in the Documents folder, enter:
ls /home/username/Documents
Here are some options you can use with the ls command:
ls -R lists all the files in the subdirectories.
ls -a shows hidden files in addition to the visible ones.
ls -lh shows the file sizes in easily readable formats, such as MB, GB, and TB.
5] cat command
Concatenate, or cat, is one of the most frequently used Linux commands. It lists,
combines, and writes file content to the standard output. To run the cat command,
type cat followed by the file name and its extension. For instance:
cat filename.txt.
Here are other ways to use the cat command:
cat > filename.txt creates a new file.
cat filename1.txt filename2.txt >
filename3.txt merges filename1.txt and filename2.txt and stores the output
in filename3.txt.
tac filename.txt displays content in reverse order.
6] cp command
Use the cp command to copy files or directories and their content. Look at the following
use cases.
To copy one file from the current directory to another, enter cp followed by the file
name and the destination directory. For example:
cp filename.txt /home/username/Documents
To copy files to a directory, enter the file names followed by the destination directory:
cp filename1.txt filename2.txt filename3.txt /home/username/Documents
To copy the content of a file to a new file in the same directory, enter cp followed by the
source file and the destination file:
cp filename1.txt filename2.txt
To copy an entire directory, pass the -R flag before typing the source directory, followed
by the destination directory:
cp -R /home/username/Documents /home/username/Documents_backup
7] mv command
The primary use of the mv command is to move and rename files and directories.
Additionally, it doesn’t produce an output upon execution.
Simply type mv followed by the filename and the destination directory. For example,
you want to move filename.txt to the /home/username/Documents directory:
mv filename.txt /home/username/Documents.
You can also use the mv command to rename a file:
mv old_filename.txt new_filename.txt
8] mkdir command
Use the mkdir command to create one or multiple directories at once and set
permissions for each of them. The user executing this command must have the privilege
to make a new folder in the parent directory, or they may receive a permission denied
error.
Here’s the basic syntax:
mkdir [option] directory_name
For example, you want to create a directory called Music:
mkdir Music
To make a new directory called Songs inside Music, use this command:
mkdir Music/Songs
The mkdir command accepts many options, such as:
-p or –parents create a directory between two existing folders. For
example, mkdir -p Music/2020/Songs will make the new “2020” directory.
-m sets the file permissions. For instance, to create a directory with full read,
write, and execute permissions for all users, enter mkdir -m777 directory_name.
-v prints a message for each created directory.
9] rmdir command
To permanently delete an empty directory, use the rmdir command. Remember that
the user running this command should have sudo privileges in the parent directory.
For example, you want to remove an empty subdirectory named personal1 and its main
folder mydir:
rmdir -p mydir/personal1
10] rm command
The rm command is used to delete files within a directory. Make sure that the user
performing this command has write permissions.
Remember the directory’s location as this will remove the file(s) and you can’t undo it.
Here’s the general syntax:
rm filename
To remove multiple files, enter the following command:
rm filename1 filename2 filename3
Here are some acceptable options you can add:
-i prompts system confirmation before deleting a file.
-f allows the system to remove without a confirmation.
-r deletes files and directories recursively.
15] df command
Use the df command to report the system’s disk space usage, shown in percentage and
kilobyte (KB). Here’s the general syntax:
df [options] [file]
For example, enter the following command if you want to see the current directory’s
system disk space usage in a human-readable format:
df -h
These are some acceptable options to use:
df -m displays information on the file system usage in MBs.
df -k displays file system usage in KBs.
df -T shows the file system type in a new column.
16] du command
If you want to check how much space a file or a directory takes up, use
the du command. You can run this command to identify which part of the system uses
the storage excessively.
Remember, you must specify the directory path when using the du command. For
example, to check /home/user/Documents enter:
du /home/user/Documents
Adding a flag to the du command will modify the operation, such as:
-s offers the total size of a specified folder.
-m provides folder and file information in MB
k displays information in KB.
-h informs the last modification date of the displayed folders and files.
38] su command
The switch user or su command allows you to run a program as a different user. It
changes the administrative account in the current log-in session. This command is
especially beneficial for accessing the system through SSH or using the GUI display
manager when the root user is unavailable.
Here’s the general syntax of the command:
su [options] [username [argument]]
When executed without any option or argument, the su command runs through root
privileges. It will prompt you to authenticate and use the sudo privileges temporarily.
Here are some acceptable options to use:
-p or –preserve-environment keeps the same shell environment, consisting of
HOME, SHELL, USER, and LOGNAME.
-s or –shell lets you specify a different shell environment to run.
-l or –login runs a login script to switch to a different username. Executing it
requires you to enter the user’s password.
39] htop command
The htop command is an interactive program that monitors system resources and server
processes in real time. It is available on most Linux distributions, and you can install it
using the default package manager.
Compared to the top command, htop has many improvements and additional features,
such as mouse operation and visual indicators.
To use it, run the following command:
htop [options]
You can also add options, such as:
-d or –delay shows the delay between updates in tenths of seconds.
-C or –no-color enables the monochrome mode.
-h or –help displays the help message and exit.
40] ps command
The process status or ps command produces a snapshot of all running processes in your
system. The static results are taken from the virtual files in the /proc file system.
Executing the ps command without an option or argument will list the running
processes in the shell along with:
The unique process ID (PID)
The type of the terminal (TTY)
The running time (TIME)
The command that launches the process (CMD)
Here are some acceptable options you can use:
-T displays all processes associated with the current shell session.
-u username lists processes associated with a specific user.
-A or -e shows all the running processes.