Lesson 6 - Basic Linux Commands on Ubuntu
28/06/2024 - 5 phút
In this lesson, we will learn about the basic Linux commands for navigating directories, managing files and directories, as well as viewing and editing files. These commands are essential for effectively operating and managing a Linux system.
1. Navigating Directories
Command | Description | Example |
---|---|---|
ls | List files and directories in the current directory | ls |
cd | Change the current working directory | cd /home/user/Documents |
pwd | Display the full path of the current directory | pwd |
Detailed Examples:
List Files and Directories (
ls
):ls
This command lists all files and directories in the current directory.
Documents Downloads Music Pictures Videos
To display detailed information including file permissions, owner, size, and modification date:
ls -l
drwxr-xr-x 2 user user 4096 Apr 10 09:00 Documents -rw-r--r-- 1 user user 512 Apr 8 12:30 file1.txt
To display hidden files (those starting with a dot):
ls -a
.bashrc .profile Documents Downloads
Change Directory (
cd
):cd /home/user/Documents
This command changes the current working directory to
/home/user/Documents
.cd ..
This command moves up one directory level.
cd ~
This command changes the directory to the user’s home directory.
Print Working Directory (
pwd
):pwd
This command displays the full path of the current directory.
/home/user/Documents
2. Managing Files and Directories
Command | Description | Example |
---|---|---|
cp | Copy files or directories from one location to another | cp file1.txt /home/user/backup/ |
mv | Move or rename files and directories | mv file1.txt /home/user/Documents/ |
rm | Delete files or directories | rm file1.txt |
mkdir | Create a new directory | mkdir /home/user/newfolder |
Detailed Examples:
Copy Files and Directories (
cp
):cp file1.txt /home/user/backup/
This command copies
file1.txt
to the/home/user/backup/
directory.To copy a directory and its contents:
cp -r /home/user/Documents /home/user/backup/
This command recursively copies the
Documents
directory and its contents to the/home/user/backup/
directory.Move or Rename Files and Directories (
mv
):mv file1.txt /home/user/Documents/
This command moves
file1.txt
to the/home/user/Documents/
directory.To rename a file:
mv oldname.txt newname.txt
This command renames
oldname.txt
tonewname.txt
.To move and rename a file:
mv oldname.txt /home/user/Documents/newname.txt
This command moves
oldname.txt
to/home/user/Documents/
and renames it tonewname.txt
.Delete Files and Directories (
rm
):rm file1.txt
This command deletes
file1.txt
.To delete a directory and all its contents:
rm -r /home/user/Documents/
This command deletes the
/home/user/Documents/
directory and all files and subdirectories within it.Create a New Directory (
mkdir
):mkdir /home/user/newfolder
This command creates a new directory named
newfolder
in the/home/user/
directory.To create nested directories:
mkdir -p /home/user/newfolder/subfolder
This command creates
newfolder
andsubfolder
inside it if they do not already exist.
3. Viewing and Editing Files
Command | Description | Example |
---|---|---|
cat | Display the contents of a file | cat file1.txt |
nano | Open a file in the Nano text editor | nano file1.txt |
vim | Open a file in the Vim text editor | vim file1.txt |
3.1. Vim Editor
Command | Description | Example |
---|---|---|
vim | Open a file in the Vim text editor | vim file1.txt |
Esc | Command mode | Press Esc to enter command mode |
i | Insert mode | Press i to enter insert mode |
:wq | Save and exit | Type :wq and press Enter to save the file and exit Vim |
:q! | Exit without saving | Type :q! and press Enter to exit without saving the file |
/text | Search for text | Type /text and press Enter to search for “text” in the file |
dd | Delete the current line | Press dd to delete the current line |
yy | Copy the current line | Press yy to copy the current line |
p | Paste the copied or cut line | Press p to paste the copied or cut line below the current line |
u | Undo | Press u to undo the last action |
Ctrl + r | Redo the undone action | Press Ctrl + r to redo the undone action |
Detailed Examples:
Open a file in Vim:
vim file1.txt
Command mode and insert mode in Vim:
- Press
Esc
to enter command mode. - Press
i
to enter insert mode.
- Press
Save and exit Vim:
- Type
:wq
and pressEnter
to save the file and exit. - Type
:q!
and pressEnter
to exit without saving.
- Type
Search and replace text:
- Type
/text
and pressEnter
to search for “text”. - To replace text, use the command
:%s/oldtext/newtext/g
.
- Type
Delete, copy, and paste lines:
- Press
dd
to delete the current line. - Press
yy
to copy the current line. - Press
p
to paste the copied or cut line below the current line.
- Press
Undo and redo actions:
- Press
u
to undo the last action. - Press
Ctrl + r
to redo the undone action.
- Press
3.2. Nano Editor
Command | Description | Example |
---|---|---|
nano | Open a file in the Nano text editor | nano file1.txt |
Ctrl + O | Save the file | Press Ctrl + O , then press Enter to save the file |
Ctrl + X | Exit Nano | Press Ctrl + X to exit Nano |
Ctrl + K | Cut text | Move the cursor to the line to cut and press Ctrl + K |
Ctrl + U | Paste text | Move the cursor to the position to paste and press Ctrl + U |
Ctrl + W | Search for text | Press Ctrl + W , type the search term, and press Enter |
Ctrl + \ | Replace text | Press Ctrl + \ , type the search term and the replacement term, then press Enter |
Ctrl + G | Display help | Press Ctrl + G to display the Nano help menu |
Detailed Examples:
Open a file in Nano:
nano file1.txt
Save a file in Nano:
- Press
Ctrl + O
. - Press
Enter
to confirm saving the file.
- Press
Exit Nano:
- Press
Ctrl + X
.
- Press
Cut and paste text:
- To cut, move the cursor to the line to cut and press
Ctrl + K
. - To paste, move the cursor to the position to paste and press
Ctrl + U
.
- To cut, move the cursor to the line to cut and press
Search and replace text:
- Press
Ctrl + W
to search for a term.- Press
Ctrl + \
to replace a term.
Display help:
- Press
Ctrl + G
to display the Nano help menu.
- Press
Conclusion
In this lesson, you have learned the basic commands to navigate directories, manage files and directories, as well as view and edit files on a Linux system. These commands are essential for effectively operating and managing your system. Practice these commands regularly to become proficient in using them.