Terminal Basics β Introduction to Using the Command Line
The Terminal, also called the command line, console, or shell, is a powerful tool for interacting with your operating system via text-based commands. It is commonly used in Linux and macOS systems, though Windows has its own terminal, such as Command Prompt or PowerShell.
Using the terminal can give you greater control over your system, and many tasks that seem complex or tedious in a GUI can be done efficiently via the command line.
________________________________________
1. Basic Terminal Commands
πΉ Navigating the File System
β’ pwd β Print Working Directory
o Shows the current directory you're in.
o Example:
o pwd
β’ ls β List Files
o Lists the files and directories in the current directory.
o Example:
o ls
β’ cd β Change Directory
o Changes the current directory to a specified one.
o Example:
o cd Documents
β’ cd .. β Go Up One Directory
o Moves you up one level in the directory structure.
o Example:
o cd ..
β’ cd / β Go to Root Directory
o Takes you to the root directory (the top-most directory).
o Example:
o cd /
________________________________________
πΉ Working with Files and Directories
β’ touch <filename> β Create a New File
o Creates a new, empty file.
o Example:
o touch file.txt
β’ mkdir <directory_name> β Make a New Directory
o Creates a new directory.
o Example:
o mkdir my_folder
β’ rm <file_name> β Remove a File
o Deletes a file.
o Example:
o rm file.txt
β’ rmdir <directory_name> β Remove an Empty Directory
o Deletes an empty directory.
o Example:
o rmdir my_folder
β’ rm -r <directory_name> β Remove a Directory and Its Contents
o Deletes a directory and all its contents (use with caution).
o Example:
o rm -r my_folder
β’ cp <source> <destination> β Copy Files or Directories
o Copies a file or directory to a new location.
o Example:
o cp file.txt /home/user/Documents/
β’ mv <source> <destination> β Move or Rename Files
o Moves or renames files or directories.
o Example:
o mv file.txt /home/user/Documents/renamed_file.txt
________________________________________
πΉ File Permissions
β’ chmod β Change File Permissions
o Changes the permissions of a file or directory. Permissions define who can read, write, or execute a file.
o Example (to make a file executable):
o chmod +x script.sh
β’ chown β Change File Owner
o Changes the ownership of a file or directory.
o Example:
o sudo chown user:user file.txt
________________________________________
2. File Search and Viewing
πΉ Search for Files
β’ find β Search for Files
o Searches for files and directories in a specified location.
o Example (searching for .txt files in the current directory):
o find . -name "*.txt"
β’ grep β Search Inside Files
o Searches for a pattern in the content of files.
o Example (search for "hello" in all .txt files):
o grep "hello" *.txt
πΉ View File Content
β’ cat β Concatenate and Display File
o Displays the content of a file.
o Example:
o cat file.txt
β’ less β View File Content with Scrolling
o Views large files with scrolling and searching functionality.
o Example:
o less file.txt
β’ head β View Beginning of a File
o Displays the first 10 lines of a file by default.
o Example:
o head file.txt
β’ tail β View End of a File
o Displays the last 10 lines of a file by default.
o Example:
o tail file.txt
________________________________________
3. System Information
πΉ System Resource Usage
β’ top β System Monitor
o Displays real-time information about processes, CPU usage, and memory consumption.
o Example:
o top
β’ htop β Enhanced System Monitor
o Similar to top, but with an easier-to-read interface and additional features.
o Example:
o htop
πΉ Disk Usage
β’ df β Disk Free
o Shows disk space usage for mounted file systems.
o Example:
o df -h
β’ du β Disk Usage of Files/Directories
o Displays disk space used by files and directories.
o Example:
o du -sh *
________________________________________
4. Process Management
πΉ Running Processes
β’ ps β Process Status
o Displays information about the currently running processes.
o Example:
o ps aux
β’ kill β Terminate a Process
o Terminates a running process using its Process ID (PID).
o Example:
o kill 1234Β # where 1234 is the PID
β’ killall β Kill All Processes by Name
o Terminates all processes with a given name.
o Example:
o killall firefox
________________________________________
5. Using Sudo
β’ sudo β Superuser Do
o Allows you to run commands with superuser (root) privileges.
o Example:
o sudo apt updateΒ # Updates system packages
________________________________________
6. Package Management (Linux)
πΉ Installing Software
β’ apt-get install <package_name> β Install Software
o Installs a package from the system's repository.
o Example:
o sudo apt-get install vim
πΉ Updating the System
β’ apt-get update β Update Package List
o Updates the list of available packages.
o Example:
o sudo apt-get update
β’ apt-get upgrade β Upgrade Installed Packages
o Upgrades all installed packages to their latest versions.
o Example:
o sudo apt-get upgrade
πΉ Removing Software
β’ apt-get remove <package_name> β Remove Software
o Removes a package but keeps its configuration files.
o Example:
o sudo apt-get remove vim
β’ apt-get purge <package_name> β Completely Remove Software
o Removes a package and its configuration files.
o Example:
o sudo apt-get purge vim
________________________________________
7. Redirection and Piping
πΉ Redirecting Output
β’ > β Redirect Output to a File
o Redirects command output to a file.
o Example (output to output.txt):
o ls > output.txt
β’ >> β Append Output to a File
o Appends command output to a file.
o Example:
o echo "Hello World" >> file.txt
πΉ Piping Commands
β’ | β Pipe Output to Another Command
o Redirects the output of one command as input to another.
o Example (list files and search for .txt files):
o ls | grep ".txt"
________________________________________
8. Environment Variables
β’ echo $PATH β Display Environment Variables
o Displays the value of the environment variable PATH.
o Example:
o echo $PATH
β’ export <variable_name>=<value> β Set Environment Variables
o Sets an environment variable for the current session.
o Example:
o export MY_VAR="Hello"