DEV Community

Cover image for Linux Commands for Beginners
Harsh Mishra
Harsh Mishra

Posted on

Linux Commands for Beginners

Linux is a robust and versatile operating system that offers a powerful command-line interface (CLI) for managing various tasks. For beginners, understanding basic Linux commands is crucial to efficiently navigating and operating within the Linux environment. This guide provides an introduction to essential Linux commands, categorized by their functionality, complete with descriptions, syntax, and multiple examples to demonstrate different ways of using each command.

Introduction to Linux Commands

Linux commands are text-based instructions that allow you to interact with your operating system. Whether you're managing files, checking system status, or installing software, these commands are your gateway to effective Linux usage.

Categories of Linux Commands

1. File and Directory Management Commands

File and directory management are core tasks in Linux. These commands help you navigate the file system, manage files and directories, and organize your system effectively.

a. ls Command

  • Description: The ls command is used to list the contents of a directory. It displays files and directories within the specified path, allowing you to see what’s inside a directory at a glance.

  • Syntax:

  ls [options] [directory]
Enter fullscreen mode Exit fullscreen mode
  • Examples:
  1. Basic listing of files and directories:

     ls
    

    Lists all files and directories in the current directory.

  2. Long format listing with details:

     ls -l
    

    Provides a detailed listing, including file permissions, number of links, owner, group, size, and the last modified date.

  3. Listing hidden files:

     ls -a
    

    Includes hidden files (those starting with a dot .).

  4. Combining options to list all files with detailed information:

     ls -la /home/user
    

    Lists all files, including hidden ones, in the /home/user directory with detailed information.

  5. Sorting files by modification time:

     ls -lt
    

    Lists files sorted by modification time, with the most recently modified files appearing first.

  6. Listing files in a specific directory:

     ls /var/log
    

    Lists the contents of the /var/log directory.

b. cd Command

  • Description: The cd (change directory) command is used to navigate between directories in the file system.

  • Syntax:

  cd [directory]
Enter fullscreen mode Exit fullscreen mode
  • Examples:
  1. Change to a specific directory:

     cd /home/user/Documents
    

    Moves to the /home/user/Documents directory.

  2. Move up one directory level:

     cd ..
    

    Moves to the parent directory.

  3. Return to the home directory:

     cd ~
    

    Takes you back to your home directory.

  4. Change to the root directory:

     cd /
    

    Switches to the root directory of the file system.

  5. Navigate to a directory with spaces in its name:

     cd 'My Projects'
    

    Changes to a directory named "My Projects" by enclosing it in quotes.

c. pwd Command

  • Description: The pwd (print working directory) command displays the full path of the current working directory.

  • Syntax:

  pwd
Enter fullscreen mode Exit fullscreen mode
  • Examples:
  1. Display the current directory:

     pwd
    

    Outputs the full path of the current working directory.

d. mkdir Command

  • Description: The mkdir command is used to create new directories.

  • Syntax:

  mkdir [options] directory_name
Enter fullscreen mode Exit fullscreen mode
  • Examples:
  1. Create a single directory:

     mkdir new_folder
    

    Creates a directory named new_folder.

  2. Create multiple directories:

     mkdir dir1 dir2 dir3
    

    Creates dir1, dir2, and dir3 in the current location.

  3. Create a directory with parent directories:

     mkdir -p /home/user/Projects/2024/January
    

    Creates 2024 and January directories under /home/user/Projects/ if they don't exist.

  4. Set directory permissions at creation:

     mkdir -m 755 new_folder
    

    Creates new_folder with permissions 755 (read, write, execute for owner, read and execute for group and others).

e. rmdir Command

  • Description: The rmdir command is used to remove empty directories.

  • Syntax:

  rmdir [options] directory_name
Enter fullscreen mode Exit fullscreen mode
  • Examples:
  1. Remove a single empty directory:

     rmdir empty_folder
    

    Deletes the empty_folder directory if it is empty.

  2. Remove multiple empty directories:

     rmdir dir1 dir2 dir3
    

    Removes dir1, dir2, and dir3 if they are all empty.

  3. Remove a directory and its parent directories:

     rmdir -p /home/user/Projects/2024/January
    

    Removes the January directory and its parent directories 2024 and Projects if they become empty after the deletion.

f. cp Command

  • Description: The cp command is used to copy files and directories from one location to another.

  • Syntax:

  cp [options] source destination
Enter fullscreen mode Exit fullscreen mode
  • Examples:
  1. Copy a file to another location:

     cp file.txt /home/user/Documents
    

    Copies file.txt to the /home/user/Documents directory.

  2. Copy a file with a new name:

     cp file.txt newfile.txt
    

    Creates a copy of file.txt with the name newfile.txt in the current directory.

  3. Copy multiple files to a directory:

     cp file1.txt file2.txt /home/user/Documents
    

    Copies file1.txt and file2.txt to the /home/user/Documents directory.

  4. Copy a directory and its contents:

     cp -r /home/user/old_directory /home/user/new_directory
    

    Recursively copies old_directory and all its contents to new_directory.

  5. Preserve attributes while copying:

     cp -p file.txt /backup/
    

    Copies file.txt to /backup/, preserving the original file attributes.

g. mv Command

  • Description: The mv command is used to move or rename files and directories.

  • Syntax:

  mv [options] source destination
Enter fullscreen mode Exit fullscreen mode
  • Examples:
  1. Move a file to a new directory:

     mv file.txt /home/user/Documents
    

    Moves file.txt to /home/user/Documents.

  2. Rename a file:

     mv oldname.txt newname.txt
    

    Renames oldname.txt to newname.txt.

  3. Move and rename a file:

     mv file.txt /home/user/Documents/newfile.txt
    

    Moves file.txt to /home/user/Documents and renames it to newfile.txt.

  4. Move multiple files to a directory:

     mv file1.txt file2.txt /home/user/Documents
    

    Moves file1.txt and file2.txt to /home/user/Documents.

h. rm Command

  • Description: The rm command is used to remove files and directories.

  • Syntax:

  rm [options] file_or_directory
Enter fullscreen mode Exit fullscreen mode
  • Examples:
  1. Remove a single file:

     rm file.txt
    

    Deletes file.txt.

  2. Remove multiple files:

     rm file1.txt file2.txt
    

    Deletes file1.txt and file2.txt.

  3. Remove a directory and its contents:

     rm -r directory_name
    

    Recursively deletes directory_name and all its contents.

  4. Forcefully remove a file:

     rm -f protected_file.txt
    

    Removes protected_file.txt without prompting for confirmation.

  5. Remove all files in a directory:

     rm -rf /path/to/directory/*
    

    Deletes all files within the specified directory, including subdirectories.

i. touch Command

  • Description: The touch command is used to create empty files or update the timestamp of existing files.

  • Syntax:

  touch [options] file_name
Enter fullscreen mode Exit fullscreen mode
  • Examples:
  1. Create a new empty file:

     touch newfile.txt
    

    Creates an empty file named newfile.txt.

  2. Update the timestamp of an existing file:

     touch existingfile.txt
    

    Updates the last modified timestamp of existingfile.txt.

  3. Create multiple files at once:

     touch file1.txt file2.txt
    

    Creates two empty files, file1.txt and file2.txt.

j. cat Command

  • Description: The cat command is used to concatenate and display the content of files. It is commonly used to view the contents of text files.

  • Syntax:

  cat [options] file_name
Enter fullscreen mode Exit fullscreen mode
  • Examples:
  1. Display the contents of a file:

     cat file.txt
    

    Outputs the content of file.txt to the terminal.

  2. Concatenate multiple files:

     cat file1.txt file2.txt > combined.txt
    

    Combines file1.txt and file2.txt into a new file named combined.txt.

  3. Display content with line numbers:

     cat -n file.txt
    

    Displays the contents of file.txt with line numbers.

  4. Append a file to another file:

     cat file2.txt >> file1.txt
    

    Appends the contents of file2.txt to file1.txt.

k. less Command

  • Description: The less command allows you to view the content of files one screen at a time. It is useful for reading long files.

  • Syntax:

  less [options] file_name
Enter fullscreen mode Exit fullscreen mode
  • Examples:
  1. View the contents of a file:

     less file.txt
    

    Opens file.txt in less, allowing you to scroll through the content.

  2. Search for a pattern within a file:

     less file.txt
    

    Inside less, type /pattern to search for a specific pattern within file.txt.

  3. Navigate within a file:

    • Move down one line: Press j or the down arrow key.
    • Move up one line: Press k or the up arrow key.
    • Move down one page: Press the spacebar.
    • Move up one page: Press b.
  4. Quit less:

     q
    

    Press q to exit less and return to the terminal.

2. File Permissions and Ownership Commands

Managing file permissions and ownership is crucial in Linux for controlling access to files and directories. These commands allow you to modify who can read, write, or execute files and change file ownership to maintain security and organization in your system.

a. chmod Command

  • Description: The chmod command is used to change the permissions of files and directories. Permissions control who can read, write, or execute a file.

  • Syntax:

  chmod [options] mode file_name
Enter fullscreen mode Exit fullscreen mode
  • Examples:
  1. Grant read, write, and execute permissions to the owner:

     chmod u+rwx file.txt
    

    Grants the owner (user) read, write, and execute permissions on file.txt.

  2. Remove write permission from the group:

     chmod g-w file.txt
    

    Removes write permission for the group on file.txt.

  3. Set read-only permission for others:

     chmod o=r file.txt
    

    Sets read-only permission for others on file.txt.

  4. Set permissions using numeric mode (e.g., 755):

     chmod 755 script.sh
    

    Sets the permissions to rwxr-xr-x, meaning the owner has full permissions, while the group and others have read and execute permissions.

  5. Apply the same permissions to multiple files:

     chmod 644 *.txt
    

    Sets the permissions to rw-r--r-- for all .txt files in the directory.

  6. Recursively change permissions for all files and directories:

     chmod -R 700 /home/user/private
    

    Recursively applies rwx------ permissions to all files and directories within /home/user/private.

b. chown Command

  • Description: The chown command changes the ownership of a file or directory. It assigns a new owner and/or group to the specified file or directory.

  • Syntax:

  chown [options] owner[:group] file_name
Enter fullscreen mode Exit fullscreen mode
  • Examples:
  1. Change the owner of a file:

     chown newuser file.txt
    

    Changes the ownership of file.txt to newuser.

  2. Change the owner and group of a file:

     chown newuser:newgroup file.txt
    

    Changes the ownership of file.txt to newuser and the group to newgroup.

  3. Change the owner of a directory and its contents:

     chown -R newuser /home/user/documents
    

    Recursively changes the owner of /home/user/documents and all its contents to newuser.

  4. Change the group ownership only:

     chown :newgroup file.txt
    

    Changes the group ownership of file.txt to newgroup without altering the file’s owner.

c. chgrp Command

  • Description: The chgrp command changes the group ownership of a file or directory. This is useful when multiple users in the same group need access to specific files.

  • Syntax:

  chgrp [options] group_name file_name
Enter fullscreen mode Exit fullscreen mode
  • Examples:
  1. Change the group ownership of a file:

     chgrp developers file.txt
    

    Changes the group ownership of file.txt to the developers group.

  2. Change the group ownership of a directory and its contents:

     chgrp -R staff /home/user/projects
    

    Recursively changes the group ownership of /home/user/projects and all its contents to the staff group.

  3. Change the group ownership of multiple files:

     chgrp admins file1.txt file2.txt
    

    Changes the group ownership of file1.txt and file2.txt to the admins group.

3. Text Processing Commands

Text processing commands are vital for managing and analyzing text files in Linux. They allow users to search for patterns, sort data, and manipulate text effectively.

a. grep Command

  • Description: The grep command searches for lines in files that match a specified pattern. It's commonly used for finding specific text within files.

  • Syntax:

  grep [options] pattern [file_name]
Enter fullscreen mode Exit fullscreen mode
  • Examples:
  1. Search for a pattern in a file:

     grep "error" file.txt
    

    Searches for the word "error" in file.txt and prints lines containing it.

  2. Search for a pattern in multiple files:

     grep "TODO" *.txt
    

    Searches for the word "TODO" in all .txt files in the current directory.

  3. Perform a case-insensitive search:

     grep -i "success" file.txt
    

    Searches for the word "success" in file.txt, ignoring case.

b. sort Command

  • Description: The sort command arranges lines in text files either alphabetically or numerically. It is useful for organizing data in a meaningful order.

  • Syntax:

  sort [options] [file_name]
Enter fullscreen mode Exit fullscreen mode
  • Examples:
  1. Sort the contents of a file alphabetically:

     sort file.txt
    

    Sorts the lines in file.txt in alphabetical order.

  2. Sort the contents of a file in reverse order:

     sort -r file.txt
    

    Sorts the lines in file.txt in reverse alphabetical order.

  3. Sort a file numerically:

     sort -n numbers.txt
    

    Sorts the lines in numbers.txt numerically.

4. Other Useful Commands

In addition to file management and user administration, there are several other useful commands in Linux that enhance your productivity and help manage your command-line environment. These include commands for viewing command history, clearing the terminal screen, displaying text, and accessing manual pages.

a. history Command

  • Description: The history command displays a list of previously executed commands in the terminal. It helps you recall and reuse past commands without retyping them.

  • Syntax:

  history [options]
Enter fullscreen mode Exit fullscreen mode
  • Examples:
  1. Show the command history:

     history
    

    Displays a list of previously executed commands with their command numbers.

  2. Show the last 10 commands:

     history 10
    

    Displays the most recent 10 commands.

  3. Search for a specific command in history:

     history | grep "command_name"
    

    Searches for command_name in the history list.

  4. Repeat a specific command from history:

     !n
    

    Replace n with the command number from the history list to re-execute that command.

b. clear Command

  • Description: The clear command clears the terminal screen of all previous commands and outputs, providing a clean slate for new commands.

  • Syntax:

  clear
Enter fullscreen mode Exit fullscreen mode
  • Examples:
  1. Clear the terminal screen:

     clear
    

    Removes all text from the terminal window.

  2. Clear the terminal screen and scrollback buffer:

     clear && printf '\e[3J'
    

    Clears both the terminal screen and the scrollback buffer.

c. echo Command

  • Description: The echo command displays a line of text or the value of variables. It’s useful for printing messages to the terminal or debugging scripts.

  • Syntax:

  echo [options] [string]
Enter fullscreen mode Exit fullscreen mode
  • Examples:
  1. Display a simple message:

     echo "Hello, World!"
    

    Prints Hello, World! to the terminal.

  2. Display the value of a variable:

     NAME="John"
     echo "Hello, $NAME!"
    

    Prints Hello, John!, assuming NAME is set to John.

  3. Print text with special characters:

     echo -e "Line 1\nLine 2"
    

    Uses -e to enable interpretation of backslash escapes, printing Line 1 and Line 2 on separate lines.

  4. Display environment variables:

     echo $PATH
    

    Prints the current PATH environment variable.

d. man Command

  • Description: The man command displays the manual pages for other commands, providing detailed documentation on their usage, options, and syntax.

  • Syntax:

  man [options] command
Enter fullscreen mode Exit fullscreen mode
  • Examples:
  1. Show the manual page for a command:

     man ls
    

    Displays the manual page for the ls command.

  2. Search for a keyword in manual pages:

     man -k keyword
    

    Searches for keyword in the manual page descriptions.

  3. View a specific section of a manual page:

     man 5 passwd
    

    Displays the manual page for the passwd file in section 5 (which typically covers file formats).

These fundamental Linux commands are essential tools for navigating, managing, and configuring your system. Mastering them provides a strong foundation for more advanced tasks and enhances your efficiency as you work with Linux. As you continue to explore and practice, these commands will become second nature, empowering you to handle various tasks with confidence and ease.

Top comments (3)

Collapse
 
niharikaa profile image
Niharika Goulikar

Very much helpful!

Collapse
 
komsenapati profile image
K Om Senapati

Very helpful for students

Collapse
 
martinbaun profile image
Martin Baun

These could come in handy for some, nice.