DEV Community

mahpara jabbar
mahpara jabbar

Posted on

Mastering the Basics: Essential Linux Commands

Introduction:
As the backbone of many servers and systems worldwide, Linux is a crucial skill for anyone in the tech industry. When I started my journey into Linux, I realized how powerful and versatile it is, especially when combined with my networking and cloud computing knowledge.
What is Linux?
Linux is an open-source operating system that powers everything from personal computers to large-scale enterprise servers. Learning Linux is not just about mastering another OS; it's about gaining control and flexibility in managing systems efficiently.
Essential Commands for Beginners:

  • ls - List directory contents

Example: ls -la lists all files, including hidden ones, with detailed information.

  • cd - Change directory

Example: cd /var/logs navigates to the logs directory.

  • pwd - Print Working Directory

Example: Running pwd in the terminal will display the full path of the current directory, such as /home/user/documents.

  • rm - Remove (delete) a file

Example: rm file.txt deletes file.txt.

  • mkdir - Make a new directory

Example: mkdir new_folder creates a directory named new_folder.

  • rmdir - Remove an empty directory

Example: rmdir old_folder removes old_folder if it is empty.

  • rm -r - Remove a directory and its contents recursively

Example: rm -r old_folder deletes old_folder and everything inside it.

  • cp - Copy Files or Directories

Example: cp source.txt destination.txt copies source.txt to destination.txt.

  • mv - Move or Rename Files or Directories Example: mv oldname.txt newname.txt renames oldname.txt to newname.txt

Tools in Linux:

  • cat - File Viewing Utility
  • touch - File Management Utility
  • vi/vim - Text Editors
  • nano - Simple Text Editor

cat - File Viewing Utility:

It displays the contents of a file in the terminal, but it can do much more.

Command: cat filename.txt

Example: cat notes.txt shows everything written inside notes.txt.

Command:cat file1.txt >> file2.txt

Example: cat footer.txt >> report.txt adds the content of footer.txt to the end of report.txt.

Command: cat > newfile.txt

Example: cat > message.txt opens input mode where you can type a message to be saved in message.txt.

Command: cat file1.txt file2.txt

Example: cat header.txt body.txt prints both files' content one after the other.

Command: tac filename

Example: tac file1 will reverse the order of the file.

touch - File Management Utility:

Used to create new empty files or update the timestamps of existing files (last accessed or modified). Creating a placeholder file or refreshing the timestamp of a file to mark it as recently modified.

Command: touch filename.txt

Example:touch notes.txt creates a new empty file called notes.txt.

Command: touch file1.txt file2.txt file3.txt

Example:touch project1.txt project2.txt project3.txt creates three empty files.

vi/vim - Text Editors:

vi and vim (Vi IMproved) are powerful text editors in Linux that can seem complex at first, but they offer great flexibility once get the hang of their basic commands.

Opening a File

Command: vi filename.txt or vim filename.txt

Switching Modes

i (Insert Mode): Press i to enter Insert mode, where you can start typing text.
Esc (Normal Mode): Press Esc to return to Normal mode, where you can use commands.

Inserting and Editing Text

i: Insert text before the cursor.
a: Insert text after the cursor.
o: Open a new line below the current line and enter Insert mode.
x: Delete the character under the cursor.
dd: Delete the entire current line.
u: Undo the last change.
Ctrl + r: Redo the undone change.

Saving and Exiting

w: Save the current file.
q: Quit the editor. If there are unsaved changes, it will warn you.
wq: Save and quit.

Copy, Cut, and Paste

p: Paste the copied or cut text after the cursor.
dd: Cut (delete) the current line.

nano - Simple Text Editor:

nano is a simple and user-friendly text editor in Linux.

Opening a File

Command: nano filename.txt

Saving and Exiting

Ctrl + O (Write Out): Save the current file. Press Enter to confirm the filename.
Ctrl + X (Exit): Exit the editor. If there are unsaved changes, it will prompt you to save before exiting.

Top comments (0)