🐧 Day 2 - Basic Linux Navigation and File Manipulation for Beginners

Linux is a powerful and widely used operating system, especially in DevOps and server environments. Whether you're managing servers, writing shell scripts, or troubleshooting issues, mastering Linux commands is essential. Here's a guide to the most basic and commonly used Linux commands to get you started.

When you start using Linux, it’s important to first know who you are, where you are, and then learn how to move around and manage files.

Before you start creating or managing files, you need to identify yourself and understand your current location in the system

When you're using the Linux terminal, the command prompt often shows something like this:

1. Username, Hostname, and Working Directory

In the Linux terminal prompt, you often see something like:

username@hostname:working_directory$
  • username: the name of the user currently logged in.

  • hostname: the name of the computer or server.

  • working_directory: the current folder you are in.

  • $: the prompt symbol indicating a regular user (for root user, it’s #).

  • Example:

      mounika@inspiron:~$
    

    Here, mounika is the user, inspiron is the computer name, and ~ means the home directory.

    To check these yourself, you can run:

    • Your username:

        whoami
      
    • Your hostname:

        hostname
      
    • Your current directory:

        pwd
      

2. What Does $ Mean?

  • The $ symbol at the end of the prompt means you are logged in as a regular user (non-root).

  • If you see #, that means you are logged in as the root user (administrator).

Example:

    mounika@inspiron:~$  (regular user)
    root@inspiron:~#  (root user)

πŸ“‚ 2. Navigate Through Directories

Linux is like a big tree of folders. You need to learn how to move between these folders.

πŸ‘‰ This is Syntax: ls

πŸ‘‰ This is Structure: Lists files and folders in the current directory.

πŸ‘‰ This is Syntax: cd foldername

πŸ‘‰ This is Structure: Changes into the folder called foldername.

πŸ‘‰ This is Syntax: cd ..

πŸ‘‰ This is Structure: Goes back one level to the parent directory.

πŸ‘‰ This is Syntax: cd ~

πŸ‘‰ This is Structure: Goes to your home directory.


πŸ“„ 3. Create and View Files

Now that you know where you are, let’s start making and reading files.

πŸ“„ How to Create Files in Linux: All Ways Explained with Examples

MethodCommand ExampleDescription
touchtouch file.txtCreate empty file
echoecho "text" > file.txtCreate file with single line text
catcat > file.txt + Ctrl+DCreate file with multiple lines input
printfprintf "line1\nline2\n" > file.txtCreate file with formatted content
Text Editorsnano file.txt or vim file.txtCreate and edit file interactively

1. Using touch command

  • Purpose: Creates an empty file or updates the timestamp of an existing file.

  • Syntax:

      touch filename.txt
    
  • Example:

      touch myfile.txt
    

    Creates an empty file called myfile.txt if it doesn't exist.

πŸ‘‰ This is Syntax: touch filename.txt

2. Using echo command

  • Purpose: Create a file and add some text content to it.

  • Syntax:

      echo "Your text here" > filename.txt
    
  • Example:

      echo "Hello Linux!" > greetings.txt
    

    Creates greetings.txt with the content Hello Linux!.

  • Note:
    Using > will overwrite the file if it exists. Use >> to append text.


3. Using cat command

  • Purpose: Create a file and enter multiple lines manually.

  • Syntax:

      cat > filename.txt
    

    Then type text, and press Ctrl + D to save and exit.

  • Example:

      cat > notes.txt
    

    Then type:

      This is my first note.
      This is the second line.
    

    Press Ctrl + D when done.


4. Using printf command

  • Purpose: Create a file with formatted content.

  • Syntax:

      printf "Line1\nLine2\n" > filename.txt
    
  • Example:

      printf "Hello\nWorld\n" > hello.txt
    

    Creates hello.txt with two lines:

      Hello
      World
    

5. Using text editors

  • Purpose: Create and edit files interactively.

  • Common editors:

    • nano filename.txt β€” Easy-to-use text editor.

    • vim filename.txt β€” Powerful but advanced editor.

  • Example with nano:

      nano myfile.txt
    

    This opens the nano editor where you can write content, then save (Ctrl + O) and exit (Ctrl + X).


  • πŸ‘€ Viewing Files in Linux: Commands and Examples

  • 1. cat β€” View entire file content

    • Syntax:

        cat filename.txt
      
    • Example:

        cat myfile.txt
      

      Displays the full content of myfile.txt on the terminal.

    • Note: Good for small files because it prints all content at once.


2. head β€” View the first few lines of a file

  • Syntax:

      head filename.txt
    

    By default, shows the first 10 lines.

  • Example:

      head myfile.txt
    

    Shows the first 10 lines of myfile.txt.

  • View specific number of lines:

      head -n 5 myfile.txt
    

    Shows first 5 lines.


3. tail β€” View the last few lines of a file

  • Syntax:

      tail filename.txt
    

    Shows last 10 lines by default.

  • Example:

      tail myfile.txt
    

    Displays last 10 lines.

  • View specific number of lines:

      tail -n 7 myfile.txt
    

    Shows last 7 lines.

  • Follow file changes live:

      tail -f /var/log/syslog
    

    Keeps showing new lines added to the file in real time (useful for logs).


4. less β€” View file page by page (scrollable)

  • Syntax:

      less filename.txt
    
  • Example:

      less myfile.txt
    

    Opens the file for easy navigation up/down.

  • Navigation keys:

    • Press Space to go to next page

    • Press b to go back one page

    • Use arrow keys to scroll line by line

    • Press q to quit


5. more β€” View file page by page (basic)

  • Syntax:

      more filename.txt
    
  • Example:

      more myfile.txt
    

    Similar to less but with fewer features.


Summary Table

CommandPurposeExample
catView full file contentcat file.txt
headView first 10 lineshead file.txt
head -n NView first N lineshead -n 5 file.txt
tailView last 10 linestail file.txt
tail -n NView last N linestail -n 7 file.txt
tail -fFollow live file updatestail -f /var/log/syslog
lessScrollable view, page-wiseless file.txt
moreBasic scrollable viewmore file.txt

  • πŸ“ Managing Directories in Linux: Commands and Examples


    What is a Directory?

    • A directory is like a folder that holds files and other directories.

    • Directories help organize your files on Linux systems.


Common Directory Management Commands

1. Create a Directory: mkdir

  • Syntax:

      mkdir directory_name
    
  • Example:

      mkdir projects
    

    Creates a new directory named projects.

  • Create multiple directories at once:

      mkdir dir1 dir2 dir3
    
  • Create parent directories as needed:

      mkdir -p parent/child/grandchild
    

    Creates the full nested directory structure even if the parents don’t exist.


2. Change Directory: cd

  • Syntax:

      cd directory_path
    
  • Example:

      cd projects
    

    Moves into the projects directory.

  • Other useful shortcuts:

    • cd .. β†’ Move up one directory (to parent directory)

    • cd ~ or simply cd β†’ Go to your home directory

    • cd - β†’ Switch back to the previous directory


3. List Directory Contents: ls

  • Syntax:

      ls [options] [directory]
    
  • Example:

      ls
    

    Lists files and folders in the current directory.

  • Common options:

    • ls -l β†’ Detailed list with permissions, size, and modification date

    • ls -a β†’ Show all files including hidden ones (those starting with .)

    • ls -lh β†’ Human-readable sizes (KB, MB)


4. Remove a Directory: rmdir and rm

  • Remove empty directory:

      rmdir directory_name
    
  • Remove directory with files inside:

      rm -r directory_name
    

    Use -r (recursive) to delete directory and its contents.

  • Force remove without prompt:

      rm -rf directory_name
    

    Be very careful! This deletes directory and everything inside without confirmation.


5. Print Current Directory: pwd

  • Syntax:

      pwd
    
  • Example:

      pwd
    

    Shows the full path of your current working directory.


Summary Table

CommandDescriptionExample
mkdir directoryCreate new directorymkdir myfolder
mkdir -p parent/childCreate nested directoriesmkdir -p projects/app/src
cd directoryChange directorycd projects
cd ..Go to parent directorycd ..
lsList files and foldersls -la
rmdir directoryRemove empty directoryrmdir oldfolder
rm -r directoryRemove directory with contentsrm -r oldfolder
pwdPrint current directory pathpwd

  • βœ‚οΈ 5. Copy, Move, and Delete Files


    Copy Files (cp)

    • Syntax:

        cp source_file destination_file
      
    • Example:

        cp file1.txt file2.txt
      

      Copies file1.txt to file2.txt.

    • Copy directories recursively:

        cp -r folder1 folder2
      

      Copies folder1 and its contents to folder2.


Move or Rename Files (mv)

  • Syntax:

      mv old_name new_name
    
  • Example (rename):

      mv oldname.txt newname.txt
    
  • Example (move to directory):

      mv filename.txt /path/to/destination/
    

Delete Files (rm)

  • Syntax:

      rm filename
    
  • Example:

      rm file.txt
    
  • Delete directories recursively:

      rm -r myfolder
    
  • Force delete without prompt:

      rm -rf myfolder
    
  • βœ… Conclusion

    These basic commands are the building blocks of using Linux. Once you’re confident with them, you’ll be able to:

    • Explore directories πŸšΆβ€β™€οΈ

    • Create files and folders πŸ“

    • Organize your data πŸ—‚οΈ

    • Understand your system better πŸ’»

Practice them regularly, and Linux will become your best friend!

10
Subscribe to my newsletter

Read articles from mounika pogakula directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

mounika pogakula
mounika pogakula

Hi, I'm Mounika Pogakula, a passionate DevOps enthusiast transitioning into tech with a strong foundation in Linux, Networking, and Cloud fundamentals. I hold a B.Sc. in Computers and a Networking Essentials certification. I’m diving deep into tools like Git, Docker, Jenkins, Kubernetes, and AWS, while sharing my real-world learning journey, hands-on practice, and beginner-friendly insights here on Hashnode. πŸ“š I'm especially focused on simplifying complex topics, documenting my progress, and building high-impact projects that help meβ€”and othersβ€”grow in the DevOps space. Let’s learn, build, and grow together πŸ’»βœ¨