Basic Linux commands

Basic Linux commands

1. Navigation Commands

  • pwd: Prints the current working directory (shows where you are in the system).

  • cd <directory>: Changes the current directory to the specified one.

  • ls: Lists the files and directories in the current directory.

    • ls -l: Lists files in long format (includes permissions, ownership, size, and modification time).

    • ls -a: Lists all files, including hidden ones (those starting with a dot).

  • cd ..: Moves up one directory level.

2. File and Directory Operations

  • touch <file>: Creates an empty file with the specified name.

  • mkdir <directory>: Creates a new directory.

  • rm <file>: Removes (deletes) a file.

    • rm -r <directory>: Removes a directory and its contents recursively.

    • rm -f <file>: Forces removal without asking for confirmation.

  • cp <source> <destination>: Copies a file or directory to another location.

    • cp -r <source> <destination>: Copies a directory recursively.
  • mv <source> <destination>: Moves or renames a file or directory.

3. Viewing Files

  • cat <file>: Displays the contents of a file.

  • more <file>: Displays file contents one screen at a time.

  • less <file>: Similar to more but allows scrolling both forward and backward.

  • head <file>: Shows the first 10 lines of a file.

  • tail <file>: Shows the last 10 lines of a file.

    • tail -f <file>: Displays the last 10 lines and updates as the file changes (useful for logs).

4. File Permissions

  • chmod <permissions> <file>: Changes file permissions (e.g., chmod 755 file).

  • chown <user>:<group> <file>: Changes the ownership of a file or directory.

5. Searching

  • find <directory> -name <filename>: Searches for files in a directory.

  • grep <pattern> <file>: Searches for a specific pattern inside a file.

    • grep -r <pattern> <directory>: Recursively searches in all files within a directory.

6. System Monitoring

  • top: Displays real-time system resource usage (CPU, memory, processes).

  • df: Shows the disk space usage of file systems.

    • df -h: Shows disk space in a human-readable format (e.g., GB, MB).
  • free: Displays memory usage.

  • ps: Lists running processes.

    • ps aux: Lists all running processes with detailed information.

7. File Compression/Decompression

  • tar -czvf <archive.tar.gz> <directory>: Compresses a directory into a tarball (.tar.gz).

  • tar -xzvf <archive.tar.gz>: Extracts a tarball (.tar.gz) archive.

  • zip <archive.zip> <file(s)>: Compresses files into a .zip archive.

  • unzip <archive.zip>: Extracts a .zip archive.

8. Networking

  • ping <hostname>: Tests connectivity to another system.

  • ifconfig (or ip a): Displays network interface configurations.

  • curl <url>: Fetches data from a URL (useful for checking web pages, APIs, etc.).

  • ssh <user>@<hostname>: Connects to a remote system via SSH.

  • scp <file> <user>@<hostname>:<path>: Copies a file to/from a remote server using SSH.

9. Package Management (for distributions like Ubuntu/Debian)

  • sudo apt update: Updates the list of available packages.

  • sudo apt install <package>: Installs a package.

  • sudo apt upgrade: Upgrades all installed packages.

  • sudo apt remove <package>: Removes a package.

10. Process Management

  • kill <PID>: Terminates a process by its Process ID (PID).

  • killall <process_name>: Terminates all processes with a given name.

  • bg: Resumes a suspended process in the background.

  • fg: Brings a background process to the foreground.

11. System Shutdown and Reboot

  • shutdown -h now: Shuts down the system immediately.

  • reboot: Reboots the system.

12. User Management (Admin)

  • whoami: Displays the current logged-in user.

  • useradd <username>: Creates a new user.

  • passwd <username>: Changes the password for a user.

  • sudo <command>: Executes a command with superuser (root) privileges.