Linux offers a powerful command-line interface, which can be intimidating for beginners. This guide covers 20 basic Linux commands that every beginner should know to navigate and manage the Linux file system effectively.
1. ls
Lists files and directories in the current directory.
Example:
Input:
ls
Output:
Desktop Documents Downloads Pictures Videos
The ls
command simply lists all files and directories in the current folder. You can add options like -l
for a detailed list or -a
to show hidden files.
2. cd
Changes the current directory.
Example:
Input:
cd Documents
Output:
(no output, but changes directory to Documents if it exists)
The cd
command changes your location to the specified directory. For example, cd /path/to/directory
moves you to that directory.
3. pwd
Displays the path of the current working directory.
Example:
Input:
pwd
Output:
/home/username/Documents
This command shows the absolute path to your current directory, which is useful for verifying where you are within the file system.
4. cp
Copies files or directories from one location to another.
Example:
Input:
cp file.txt /path/to/destination/
Output:
(no output, but file.txt is copied to the specified destination)
The cp
command copies file.txt
to the specified destination folder. Use -r
for copying directories.
5. mv
Moves or renames files and directories.
Example (Moving a file):
Input:
mv file.txt /path/to/destination/
Output:
(no output, but file.txt is moved to the destination)
Example (Renaming a file):
Input:
mv oldname.txt newname.txt
Output:
(no output, but oldname.txt is renamed to newname.txt)
The mv
command can be used to move files to a new location or rename them.
6. rm
Removes files or directories (use with caution as this is irreversible).
Example:
Input:
rm file.txt
Output:
(no output, but file.txt is deleted)
Use -r
to remove directories and their contents.
7. mkdir
Creates a new directory.
Example:
Input:
mkdir new_directory
Output:
(no output, but new_directory is created)
The mkdir
command makes a new directory with the specified name.
8. rmdir
Removes an empty directory.
Example:
Input:
rmdir empty_directory
Output:
(no output, but empty_directory is deleted if it was empty)
This command only works if the directory is empty. Use rm -r
for non-empty directories.
…
20. man
Displays the manual for a command.
Example:
Input:
man ls
Output:
LS(1) User Commands LS(1)
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is speci‐
fied.
...
The man
command shows detailed information about a command, including options and usage. For example, man ls
shows the manual for the ls
command.
Each command example above includes both the input (what the user types) and the output (what the terminal displays). This format should make it easier for readers to understand how each command works and what they can expect when they run it.
FAQ
What is the difference between cp
and mv
?
cp
: This command is used to copy files or directories from one location to another. The original file remains in its place.mv
: This command is used to move files or directories from one location to another. It can also be used to rename files or directories.
How can I view hidden files in a directory?
To view hidden files, use the ls
command with the -a
option:
ls -a
This will list all files, including those that are hidden (files starting with a dot .
).
How do I safely remove a directory and its contents?
To safely remove a directory and all its contents, use the rm
command with the -r
option:
rm -r directory_name
Be cautious with this command, as it will permanently delete the directory and its contents.
How can I find the manual for a specific command?
Use the man
command followed by the command name to view its manual. For example, to view the manual for the ls
command:
man ls
This will display detailed information about the command, including its options and usage.
What should I do if I accidentally delete a file?
Once a file is deleted using the rm
command, it cannot be recovered through the command line. It’s advisable to regularly back up important files to prevent data loss.