Linux Basic Commands 101: A Beginner's Guide

September 10, 2024 (2mo ago)

Linux terminal

Linux is one of the most popular operating systems, known for its flexibility, security, and efficiency. Whether you're a developer, sysadmin, or simply curious about tech, learning Linux commands can open up a whole new world of possibilities. In this guide, we’ll cover the most essential Linux commands every beginner should know.

Why Learn Linux Commands?

While Linux offers graphical user interfaces (GUIs) like Ubuntu's Unity or GNOME, the real power of Linux lies in its command-line interface (CLI). Mastering the command line allows you to:

  • Automate tasks through scripts.
  • Troubleshoot and manage systems efficiently.
  • Perform system administration tasks remotely.

Now, let’s dive into the basic Linux commands.

1. pwd – Print Working Directory

When you open a terminal, you're in a specific folder (directory). The pwd command tells you which directory you're in.

$ pwd /home/username This command is particularly useful when you're navigating through multiple directories and need to know your current location.

2. ls – List Files

The ls command lists the files and directories within your current directory. You can use various options to customize the output:

$ ls Desktop Documents Downloads

$ ls -l # Detailed listing drwxr-xr-x 2 user user 4096 Sep 9 14:29 Desktop -rw-r--r-- 1 user user 2890 Sep 10 10:14 file.txt Some useful flags:

  • -a: Shows hidden files (those starting with a dot).
  • -l: Displays file details like permissions, owner, and modification time.

3. cd – Change Directory

The cd command allows you to move between directories.

$ cd Documents $ cd /home/username/Downloads $ cd .. # Move up one level If you ever get lost, just combine pwd with cd to navigate back to familiar directories.

4. mkdir – Make Directory

To create a new directory (folder), use mkdir followed by the directory name:

$ mkdir new_folder $ ls Desktop Documents Downloads new_folder

5. rm – Remove Files/Directories

Use the rm command to delete files. Be cautious, as Linux doesn't have a recycle bin for CLI operations:

$ rm filename.txt To remove directories, use the -r (recursive) option:

$ rm -r directory_name

6. cp – Copy Files and Directories

The cp command copies files from one location to another:

$ cp file.txt /home/username/Documents/ To copy entire directories, add the -r flag:

$ cp -r folder_name /home/username/backup/

7. mv – Move/Rename Files

The mv command is used for both moving and renaming files:

$ mv oldname.txt newname.txt # Rename a file $ mv file.txt /home/username/Documents/ # Move a file

8. cat – Display File Contents

The cat command is short for "concatenate" and is used to display the contents of a file:

$ cat file.txt

9. nano and vim – Text Editors

Sometimes you'll need to edit files directly in the terminal. Two common command-line editors are nano (beginner-friendly) and vim (more advanced).

$ nano file.txt # Opens file in the Nano editor Press Ctrl + O to save and Ctrl + X to exit Nano.

10. chmod – Change Permissions

Permissions in Linux control who can read, write, or execute files. The chmod command lets you change file permissions.

$ chmod 755 script.sh Here, 755 means the file owner can read, write, and execute (7), and everyone else can read and execute (5).

11. ps and top – Process Management

The ps command lists the processes currently running:

$ ps PID TTY TIME CMD 1234 pts/1 00:00:00 bash 5678 pts/1 00:00:05 top For real-time monitoring of processes, use the top command:

$ top This displays CPU usage, memory usage, and other details about running processes.

12. sudo – Execute Commands as Superuser

Some commands require root (superuser) privileges. To run them, use sudo:

$ sudo apt update $ sudo rm -r /restricted_directory Be cautious when using sudo, as it can modify important system files.

13. df and du – Disk Space

  • The df command shows disk space usage of file systems:

    $ df -h # The -h flag makes the output human-readable

  • The du command displays disk usage of files and directories:

    $ du -sh /home/username # Shows the size of a directory

14. grep – Search Text

The grep command is a powerful tool for searching within files:

$ grep 'search_term' file.txt You can also search recursively within directories:

$ grep -r 'search_term' /path/to/directory/

15. history – View Command History

The history command shows a list of previously executed commands:

$ history This is useful for recalling past commands without retyping them.

Conclusion

Learning these basic Linux commands will give you the foundation to navigate and manage Linux systems efficiently. As you grow more comfortable with these commands, you'll discover even more powerful tools that Linux offers. So keep practicing, and before long, the command line will become second nature!