hexdefender
Email
  • Introduction to Linux
    • Overview
    • Linux Kernel
    • Linux Distros
    • Introduction to Kali Linux
    • Install Kali on VirtualBox
    • Install Kali on AWS
  • Linux Commands
    • Linux File Systems
    • Basic File and Directory commands
    • File Permissions and Ownerships
    • System Commands in Linux
    • Text Processing Commands in Linux
    • Linux Archive Utility
    • Package Management in Kali Linux
    • Networking Commands
    • Disk Utility Tools
    • Linux List of CLI Command lookup
    • Linux CLI Cheatsheet
    • Assignment
  • Networking Essentials
    • Overview
    • Networking Protocols
    • IP Addressing & Subnetting
    • DNS and DNS Security
    • Network Devices and Architecture
    • VPNs and Secure Tunnels
    • Network Address Translation (NAT) & Port Forwarding
    • Wireless Networks & Protocols
    • Cloud Networking & Security
    • Common Network Tools
  • Bash Scripting
    • Fundamentals of Bash
    • Variables, Branching and Loops
    • System Variables in Bash
    • Functions and Error Handling in Bash Scripts
    • File Handling and Text Processing
    • 5 Useful Bash Scripts for Everyday Tasks
    • Useful Assignments
  • Fundamentals of Cybersecurity
    • Introduction to Cybersecurity
    • Importance of Cybersecurity
    • Important Cybersecurity Frameworks
    • Cybersecurity Roles and Career Options
  • Penetration Testing
    • Reconnaissance and Footprinting
    • Exploitation Techniques
      • Introduction
      • Service Enumeration
      • Password Attacks
      • Exploit Discovery
      • The Art of Exploitation
      • The Pentester's guide to Metasploit
    • Post Exploitation - Malware & Escalation
  • Web Application Security
    • Common Web Vulnerabilities
    • OWASP Top 10
    • SQL Injections
    • Cross Site Scripting Attacks
    • Web Application Firewalls
    • Secure Coding Practices
  • Cryptography
    • Basic concepts of cryptography
    • Examples of Asymetric & Hashing functions
    • Public Key Infrastructure
    • Digital Signatures
    • Symmetric and Asymmetric Encryption
  • Social Engineering
    • Introduction to Social Engineering
    • Mitigation Strategies for Social Engineering
  • Digital Forensics
    • Digital Forensics Basics
    • Forensics Tools and Techniques
    • Reverse Engineering Fundamentals
    • Malware Analysis
Powered by GitBook
On this page
  1. Linux Commands

Basic File and Directory commands

Chapter 1: Basic File and Directory Commands

In this chapter, we'll cover essential Linux commands used for navigating and managing files and directories. Understanding these commands is crucial for efficiently working with the Linux filesystem.

Commands Table

Command

Common Arguments

Examples

Description

ls

-l, -a, -h, -R

ls -l (long listing), ls -a (show hidden files), ls -h (human-readable sizes), ls -R (recursive)

Lists the contents of a directory, including file details, hidden files, sizes, and recursive listings.

cd

<directory>

cd /home/user, cd .. (move up one level), cd ~ (go to home)

Changes the current working directory.

pwd

None

pwd

Prints the current working directory path.

mkdir

-p, <directory_name>

mkdir test, mkdir -p parent/child

Creates a new directory. -p creates parent directories as needed.

rmdir

<directory_name>

rmdir test

Removes an empty directory.

cp

-r, -v, -i

cp file1 file2, cp -r dir1 dir2 (copy directory), cp -i file1 file2 (prompt before overwrite)

Copies files and directories. -r for directories, -i prompts before overwrite.

mv

-i, -v

mv oldname newname, mv -i file1 file2

Moves or renames files and directories. -i prompts before overwrite.

rm

-r, -f, -i

rm file, rm -r dir (remove directory), rm -i file (prompt before deletion)

Deletes files or directories. -r for directories, -f forces deletion, -i prompts before delete.

cat

-n, <file>

cat file.txt, cat -n file.txt (number lines)

Concatenates and displays file contents.

more

None

more file.txt

Displays file contents one screen at a time.

less

None

less file.txt

Displays file contents, allowing backward and forward navigation.

Command Examples

  1. Listing Files with Details:

    ls -l

    Displays detailed information about files and directories.

  2. Changing Directories:

    cd /var/log

    Navigates to the /var/log directory.

  3. Copying a File:

    cp file1.txt /tmp/file2.txt

    Copies file1.txt to /tmp/ and renames it to file2.txt.

  4. Removing a Directory Recursively:

    rm -r test_directory

    Deletes test_directory and its contents.

  5. Viewing File Contents with Line Numbers:

    cat -n file.txt

    Displays the contents of file.txt with line numbers.

Interview Questions and Answers

  1. Q: What is the purpose of the ls command? How would you list all hidden files? A: The ls command lists files and directories in the current directory. To list hidden files, use ls -a, which includes files starting with a dot (.).

  2. Q: How would you navigate back to your home directory from any location in the terminal? A: You can navigate back to your home directory using cd ~ or simply cd without any arguments.

  3. Q: What is the difference between rm -r and rmdir? A: rm -r removes a directory and all its contents recursively, while rmdir only removes empty directories.

  4. Q: Explain the use of the mv command. Can it be used to rename files? A: The mv command is used to move or rename files and directories. To rename a file, specify the current name and the new name: mv oldname.txt newname.txt.

  5. Q: How can you safely copy files to avoid accidentally overwriting existing ones? A: Use the cp -i command, which prompts before overwriting files, allowing you to confirm each action.

  6. Q: What is the difference between cat, more, and less? A: cat displays the entire file contents at once, more shows contents one screen at a time but only allows forward navigation, and less allows both forward and backward navigation through the file.

This chapter covers the essential commands that form the foundation of working with Linux files and directories. Mastery of these commands is critical for navigating the system and managing files effectively in any Linux environment.

PreviousLinux File SystemsNextFile Permissions and Ownerships

Last updated 8 months ago