Linux Basics
Linux is a powerful open-source operating system. As a contributor, understanding basic command-line usage is key. This guide covers essential commands, system updates, and best practices for command-line work.
Basic Linux Commands
The command line is your main interface. Here are core commands you'll use often:
ls(list): Shows directory contents.ls: Lists files and directories in the current location.- Example:
lsmight outputdocuments images myproject.txt
- Example:
ls -l: Provides a "long listing" with details like permissions, size, and date.- Example:
ls -lmight show-rw-r--r-- 1 user group 1024 Jul 10 10:30 myproject.txt
- Example:
ls -a: Shows all files, including hidden ones (starting with a.).- Example:
ls -amight reveal.bashrc .config documents
- Example:
cd(change directory): Navigates between directories.cd <directory_name>: Moves to a specific directory.- Example:
cd documents
- Example:
cd ..: Moves up one directory level.- Example: If in
/home/user/documents,cd ..moves to/home/user/
- Example: If in
cd ~: Returns to your home directory.- Example: No matter where you are,
cd ~takes you to/home/user/
- Example: No matter where you are,
pwd(print working directory): Displays your current directory's full path.- Example:
pwdmight output/home/user/myproject
- Example:
mkdir(make directory): Creates a new directory.- Example:
mkdir new_folder
- Example:
rmdir(remove directory): Deletes an empty directory.- Example:
rmdir empty_folder
- Example:
touch: Creates an empty file or updates a file's timestamp.- Example:
touch new_file.txt(creates a new empty file)
- Example:
cp(copy): Copies files or directories.cp <source_file> <destination_file>: Copies a file.- Example:
cp report.docx report_backup.docx
- Example:
cp -r <source_directory> <destination_directory>: Copies a directory and its contents.- Example:
cp -r myproject myproject_copy
- Example:
mv(move): Moves or renames files/directories.- Example (move):
mv old_file.txt documents/(movesold_file.txtinto thedocumentsfolder) - Example (rename):
mv old_name.txt new_name.txt
- Example (move):
rm(remove): Deletes files or directories.rm <file_name>: Deletes a file.- Example:
rm unwanted_file.txt
- Example:
rm -r <directory_name>: Recursively deletes a directory (use with extreme caution!).- Example:
rm -r old_project_folder(Deletes the folder and everything inside it)
- Example:
cat(concatenate): Displays a file's content.- Example:
cat README.md(shows the content of the README.md file)
- Example:
less/more: Views file content page by page for large files.- Example:
less /var/log/syslog(allows you to scroll through a large log file; pressqto quit)
- Example:
grep: Searches for text patterns within files.- Example:
grep "error" /var/log/syslog(finds all lines containing "error" in the syslog file)
- Example:
man(manual): Provides documentation for commands.- Example:
man ls(shows the manual page for thelscommand; pressqto quit)
- Example:
How to Update and Upgrade the System
Keeping your system updated is vital for security and new features. For Debian-based systems (like Ubuntu), use apt:
sudo apt update: Refreshes the list of available packages from repositories.- Example:
sudo apt update(You'll see a list of package information being downloaded)
- Example:
sudo apt upgrade: Installs newer versions of all currently installed packages.- Example:
sudo apt upgrade(Prompts you to confirm the installation of available updates)
- Example:
sudo apt full-upgrade: Performs a more comprehensive upgrade, installing new packages if needed for dependencies and removing obsolete ones.- Example:
sudo apt full-upgrade(Use for major system version upgrades or whenupgradeis insufficient)
- Example:
To update:
- Open your terminal.
- Run
sudo apt update. - Then, run
sudo apt upgrade. - Optionally, use
sudo apt full-upgradefor a deeper update.
How sudo and apt Work
-
sudo(SuperUser DO):- Function: Allows you to run commands with root (administrator) privileges using your own password.
- When to use: For tasks requiring elevated permissions, like installing software, modifying system files, or managing services.
- Caution: Always be careful with
sudo; incorrect commands can harm your system.- Example:
sudo systemctl restart apache2(restarts the Apache web server, requires root privileges)
- Example:
-
apt(Advanced Package Tool):- Function: A command-line utility for managing software packages (installing, updating, removing) on Debian-based systems. It interacts with software repositories.
- When to use:
apt install <package_name>: Install new software.- Example:
sudo apt install git(installs the Git version control system)
- Example:
apt remove <package_name>: Uninstall software (leaves configuration files).- Example:
sudo apt remove firefox
- Example:
apt purge <package_name>: Uninstall software and its configuration files.- Example:
sudo apt purge apache2
- Example:
apt search <keyword>: Find packages.- Example:
apt search text editor(lists available text editor packages)
- Example:
Tips for Command-Line Navigation and Safety
Here are some best practices to make your command-line work more efficient and secure:
- Tab Completion: Press the
Tabkey to auto-complete commands, file names, or directory names. PressTabtwice to see all available options if there's more than one. - Arrow Keys: Use the
UpandDownarrow keys to cycle through your command history. This saves typing and helps recall previous commands. Ctrl+R(Reverse Search): PressCtrl+Rand start typing to search your command history for a specific command. It's incredibly useful for finding a command you ran a while ago.- Read the Manual (
man): If you're unsure about a command or its options, useman <command_name>for detailed documentation. Pressqto quit the manual. - Be Careful with
sudo: Always double-check your commands before pressing Enter when usingsudo. Commands run with root privileges can make significant, irreversible changes to your system if executed incorrectly. - Backup Regularly: Make a habit of backing up important files and configurations, especially before performing major system changes or software installations. This can save you from potential data loss.