01. Introduction to Linux Commands
Linux commands allow us to interact with the operating system using a terminal. We can create files, remove directories, move data, install software, and manage the system without using a graphical interface.
The terminal accepts commands and displays their results. This is especially useful for Linux administration, networking, and cybersecurity.
What Does a Terminal Prompt Look Like?
student@linux:~$ pwd /home/student
In this example, student is the username,
linux is the computer name, and
~ represents the user's home directory.
02. rmdir — Remove an Empty Directory
The rmdir command removes an empty
directory. It cannot remove a directory that still
contains files or other directories.
Syntax
rmdir directory_name
Example and Output
student@linux:~$ mkdir demo student@linux:~$ ls -d demo demo student@linux:~$ rmdir demo student@linux:~$ ls -d demo ls: cannot access 'demo': No such file or directory
First, we created an empty directory named
demo. The rmdir command
removed it. The final ls command
confirms that the directory no longer exists.
rmdir command normally displays no output.
03. rm — Remove Files
The rm command removes files from
the filesystem. It is commonly used to delete
files that are no longer needed.
Syntax
rm filename
Example and Output
student@linux:~$ touch notes.txt student@linux:~$ ls notes.txt notes.txt student@linux:~$ rm notes.txt student@linux:~$ ls notes.txt ls: cannot access 'notes.txt': No such file or directory
The touch command created an empty
file named notes.txt.
The rm command removed that file.
rm do not normally go to the desktop
Trash or Recycle Bin. Always check the filename
before deleting anything.
04. rm -r — Remove a Directory and Its Contents
The rm -r command removes a directory
recursively. This means it removes the directory
along with the files and subdirectories inside it.
The -r option means
recursive.
Syntax
rm -r directory_name
Example and Output
student@linux:~$ mkdir project student@linux:~$ touch project/file.txt student@linux:~$ ls project file.txt student@linux:~$ rm -r project student@linux:~$ ls -d project ls: cannot access 'project': No such file or directory
We created a directory called project
and added a file inside it. The
rm -r command removed both the file
and its parent directory.
05. rm -rf — Force Recursive Removal
The rm -rf command recursively removes
files and directories while ignoring missing files
and suppressing many confirmation prompts.
Here, -r means recursive and
-f means force.
rm -rf
command without understanding exactly what it
will remove.
Safe Practice Example
student@linux:~$ mkdir practice-folder student@linux:~$ touch practice-folder/test.txt student@linux:~$ ls practice-folder test.txt student@linux:~$ rm -rf practice-folder student@linux:~$ ls -d practice-folder ls: cannot access 'practice-folder': No such file or directory
This example removes only the temporary
practice-folder that we created.
It should not be used on important directories.
06. cp — Copy Files
The cp command copies files from
one location to another. It can also create
another copy of a file with a different name.
Syntax
cp source destination
Example and Output
student@linux:~$ echo "Hello Linux" > notes.txt student@linux:~$ cp notes.txt backup.txt student@linux:~$ ls backup.txt notes.txt student@linux:~$ cat backup.txt Hello Linux
The command copied the contents of
notes.txt into
backup.txt.
The original file still exists.
07. cp -r — Copy Directories
The cp -r command copies a directory
together with the files and subdirectories
inside it.
The -r option means recursive.
Syntax
cp -r source_directory destination_directory
Example and Output
student@linux:~$ mkdir documents student@linux:~$ touch documents/file1.txt student@linux:~$ cp -r documents documents-backup student@linux:~$ ls documents-backup file1.txt
The documents directory was copied
into a new directory called
documents-backup.
Both directories now exist.
08. mv — Move or Rename Files and Directories
The mv command is used to move files
and directories to another location.
It can also rename them.
Example 1: Rename a File
student@linux:~$ touch oldname.txt student@linux:~$ mv oldname.txt newname.txt student@linux:~$ ls newname.txt newname.txt
The file was renamed from
oldname.txt to
newname.txt.
Example 2: Move a File
student@linux:~$ mkdir backup student@linux:~$ mv newname.txt backup/ student@linux:~$ ls backup newname.txt
The file newname.txt was moved
into the backup directory.
cp,
the mv command does not normally
leave a second copy at the original location.
It may also overwrite an existing destination file.
09. sudo apt update — Refresh Package Information
The sudo apt update command downloads
the latest package information from configured
software repositories.
It helps the system discover available software versions and updates. It does not install those updates by itself.
What Does Each Part Mean?
- sudo: Run a command with elevated privileges, when authorized.
- apt: Package management tool used on Debian-based Linux systems.
- update: Refresh the available package lists.
Example and Output
student@linux:~$ sudo apt update [sudo] password for student: Hit:1 http://deb.debian.org/debian stable InRelease Reading package lists... Done Building dependency tree... Done Reading state information... Done All packages are up to date.
This example shows that the package lists were refreshed successfully. The repository addresses and messages may differ on your Linux system.
apt update
updates the package information, not the installed
software packages.
10. sudo apt upgrade — Upgrade Installed Packages
The sudo apt upgrade command upgrades
installed software packages when newer compatible
versions are available in the configured repositories.
It is commonly used after
sudo apt update.
Example and Output
student@linux:~$ sudo apt upgrade Reading package lists... Done Building dependency tree... Done Reading state information... Done Calculating upgrade... Done 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
In this example, no upgrades were available. If updates are available, APT may show a list of packages and ask for confirmation.
Difference Between Update and Upgrade
| Command | Purpose |
|---|---|
sudo apt update |
Refreshes the available package information. |
sudo apt upgrade |
Upgrades installed packages when suitable updates are available. |
11. Hands-On Linux Practice Lab
Now practice the commands you have learned. Use a test directory so you do not accidentally modify important files.
Lab 1 — Create and Copy a File
Goal: Create a file and make a backup copy.
student@linux:~$ mkdir linux-practice student@linux:~$ cd linux-practice student@linux:~/linux-practice$ echo "Linux Practice" > file.txt student@linux:~/linux-practice$ cp file.txt backup.txt student@linux:~/linux-practice$ ls backup.txt file.txt
You created a practice directory, added a file,
and copied it using cp.
Lab 2 — Rename and Remove a File
Goal: Practice mv and rm.
student@linux:~/linux-practice$ mv backup.txt saved.txt student@linux:~/linux-practice$ ls file.txt saved.txt student@linux:~/linux-practice$ rm saved.txt student@linux:~/linux-practice$ ls file.txt
You renamed backup.txt to
saved.txt, then removed it.
Lab 3 — Remove an Empty Directory
Goal: Understand how rmdir works.
student@linux:~/linux-practice$ mkdir empty-folder student@linux:~/linux-practice$ rmdir empty-folder student@linux:~/linux-practice$ ls file.txt
The empty directory was removed successfully.
pwd and ls before
deleting files. These commands help you confirm
your current directory and its contents.
12. Quick Revision
| Command | What It Does |
|---|---|
rmdir |
Removes an empty directory. |
rm |
Removes files. |
rm -r |
Removes a directory and its contents recursively. |
rm -rf |
Forces recursive removal; use carefully. |
cp |
Copies files. |
cp -r |
Copies directories and their contents. |
mv |
Moves or renames files and directories. |
sudo apt update |
Refreshes package information. |
sudo apt upgrade |
Upgrades installed packages. |
13. Additional Learning Resources
Use these references to learn more about Linux commands and practice safely.