When I first started studying computers, I was resistant to using Linux and Git, but once I got used to them, I found them extremely useful. This document is a record of frequently used commands so I don’t have to look them up or ask GPT each time.

Common Linux Commands

System Information

uname -a               # Display system information
hostname               # Show or set the hostname
whoami                 # Current logged-in user
uptime                 # System uptime and load
date                   # Current date and time
cal                    # Display a calendar
df -h                  # View disk usage
du -h                  # Check directory or file size
free -m                # View memory usage
top                    # Display dynamic process info
htop                   # Similar to top, requires installation
ps -aux                # List all running processes

File and Directory Operations

ls                     # List directory contents
ls -l                  # Show detailed information
ls -a                  # Display hidden files
cd /path/to/dir        # Change directory
pwd                    # Show the current path
mkdir new_dir          # Create a directory
rmdir empty_dir        # Remove an empty directory
rm file                # Delete a file
rm -r dir              # Delete a directory and its contents
cp source dest         # Copy files or directories
mv source dest         # Move or rename files
find /path -name "*.txt" # Search for files
locate filename        # Quickly find files
tree                   # Show directory structure as a tree (requires installation)
touch file.txt         # Create an empty file
stat file.txt          # View detailed file information
file file.txt          # Determine file type

Viewing File Contents

cat file.txt           # Display file content
tac file.txt           # Display file content in reverse order
less file.txt          # View file content in pages
more file.txt          # Similar to less
head -n 10 file.txt    # View the first 10 lines of a file
tail -n 10 file.txt    # View the last 10 lines of a file
tail -f log.txt        # Follow a log file in real-time
nl file.txt            # Display file content with line numbers

Permission Management

chmod 755 file.txt     # Change permissions
chown user file.txt    # Change file owner
chgrp group file.txt   # Change file group
umask 022              # Set default file permissions

Numeric Permission Representation: Permissions are represented by three numbers (0–7) for:

  1. Owner (User)
  2. Group
  3. Others

Each number is a sum of:

  • 4: Read (r)
  • 2: Write (w)
  • 1: Execute (x)

Examples:

  • 7 = 4 + 2 + 1 (read, write, execute)
  • 5 = 4 + 1 (read, execute)
  • 6 = 4 + 2 (read, write)

For 755:

  • Owner: Read, write, execute (rwx)
  • Group/Others: Read, execute (r-x)

Common Git Commands

Git Configuration

git config --global user.name "Your Name"               # Set global username
git config --global user.email "your.email@example.com" # Set global email
git config --list                                       # View current configuration
git config --global core.editor vim                    # Set default editor

Repository Operations

git init                          # Initialize a new Git repository
git clone <repo_url>              # Clone a remote repository locally
git status                        # View the current branch status
git log                           # View commit history
git log --oneline                 # Simplified commit history
git show <commit_hash>            # View details of a specific commit

Branch Management

git branch                        # List local branches
git branch <branch_name>          # Create a new branch
git checkout <branch_name>        # Switch to a specific branch
git checkout -b <branch_name>     # Create and switch to a new branch
git merge <branch_name>           # Merge a branch into the current branch
git branch -d <branch_name>       # Delete a local branch
git branch -D <branch_name>       # Force delete a local branch

Making Changes

git add <file>                    # Add a file to the staging area
git add .                         # Add all files in the current directory
git commit -m "commit message"    # Commit changes with a message
git commit --amend                # Modify the previous commit

Remote Repository Operations

git remote -v                     # View remote repository URLs
git remote add origin <repo_url>  # Add a remote repository
git push origin <branch_name>     # Push a branch to the remote repository
git pull origin <branch_name>     # Pull updates from the remote repository
git fetch origin                  # Fetch updates without merging

Stashing

git stash                         # Save current progress
git stash list                    # View saved stashes
git stash apply                   # Restore the most recent stash
git stash clear                   # Clear all stashes