Lesson 6 - Basic Linux Commands on Ubuntu

28/06/2024 - 5 phút

Follow  on Google News

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

CommandDescriptionExample
lsList files and directories in the current directoryls
cdChange the current working directorycd /home/user/Documents
pwdDisplay the full path of the current directorypwd

Detailed Examples:

  1. 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
    
  2. 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.

  3. Print Working Directory (pwd):

    pwd
    

    This command displays the full path of the current directory.

    /home/user/Documents
    

2. Managing Files and Directories

CommandDescriptionExample
cpCopy files or directories from one location to anothercp file1.txt /home/user/backup/
mvMove or rename files and directoriesmv file1.txt /home/user/Documents/
rmDelete files or directoriesrm file1.txt
mkdirCreate a new directorymkdir /home/user/newfolder

Detailed Examples:

  1. 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.

  2. 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 to newname.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 to newname.txt.

  3. 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.

  4. 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 and subfolder inside it if they do not already exist.

3. Viewing and Editing Files

CommandDescriptionExample
catDisplay the contents of a filecat file1.txt
nanoOpen a file in the Nano text editornano file1.txt
vimOpen a file in the Vim text editorvim file1.txt

3.1. Vim Editor

CommandDescriptionExample
vimOpen a file in the Vim text editorvim file1.txt
EscCommand modePress Esc to enter command mode
iInsert modePress i to enter insert mode
:wqSave and exitType :wq and press Enter to save the file and exit Vim
:q!Exit without savingType :q! and press Enter to exit without saving the file
/textSearch for textType /text and press Enter to search for “text” in the file
ddDelete the current linePress dd to delete the current line
yyCopy the current linePress yy to copy the current line
pPaste the copied or cut linePress p to paste the copied or cut line below the current line
uUndoPress u to undo the last action
Ctrl + rRedo the undone actionPress Ctrl + r to redo the undone action

Detailed Examples:

  1. Open a file in Vim:

    vim file1.txt
    
  2. Command mode and insert mode in Vim:

    • Press Esc to enter command mode.
    • Press i to enter insert mode.
  3. Save and exit Vim:

    • Type :wq and press Enter to save the file and exit.
    • Type :q! and press Enter to exit without saving.
  4. Search and replace text:

    • Type /text and press Enter to search for “text”.
    • To replace text, use the command :%s/oldtext/newtext/g.
  5. 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.
  6. Undo and redo actions:

    • Press u to undo the last action.
    • Press Ctrl + r to redo the undone action.

3.2. Nano Editor

CommandDescriptionExample
nanoOpen a file in the Nano text editornano file1.txt
Ctrl + OSave the filePress Ctrl + O, then press Enter to save the file
Ctrl + XExit NanoPress Ctrl + X to exit Nano
Ctrl + KCut textMove the cursor to the line to cut and press Ctrl + K
Ctrl + UPaste textMove the cursor to the position to paste and press Ctrl + U
Ctrl + WSearch for textPress Ctrl + W, type the search term, and press Enter
Ctrl + \Replace textPress Ctrl + \, type the search term and the replacement term, then press Enter
Ctrl + GDisplay helpPress Ctrl + G to display the Nano help menu

Detailed Examples:

  1. Open a file in Nano:

    nano file1.txt
    
  2. Save a file in Nano:

    • Press Ctrl + O.
    • Press Enter to confirm saving the file.
  3. Exit Nano:

    • Press Ctrl + X.
  4. 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.
  5. Search and replace text:

    • Press

    Ctrl + W to search for a term.

    • Press Ctrl + \ to replace a term.
  6. Display help:

    • Press Ctrl + G to display the Nano help menu.

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.