Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Essential Linux Commands for File and System Management

Tech 2

1. Basic Navigation

1.1 cd: Change Directory

# Change to an absolute path
cd /study/files

# Change to a relative path
cd test/

# Special directory shortcuts
cd .      # Current directory
cd ..     # Parent directory
cd /      # Root directory
cd ~      # Home directory

1.2 ls: List Directory Contents

# Basic listing
ls

# Detailed listing
ls -l

# Alias for detailed listing
ll

1.3 pwd: Print Working Directory

# Show current directory path
pwd

2. System Information

2.1 hostname: Display Host Name

hostname

2.2 who: Show Logged-in Users

who

2.3 whoami: Display Current Usenrame

whoami

2.4 ifconfig: Show Network Interface Configuration

ifconfig

2.5 ping: Test Network Connectivity

ping example.com

3. File and Dircetory Operations

3.1 mkdir: Create Directories

# Create a single directory
mkdir /usr/local/Test

# Create nested directories
mkdir -p parent/child/grandchild

3.2 touch: Create Empty Files

# Create a file with extension
touch document.txt

3.3 vi: Text Editor

# Open file for editing
vi document.txt

# Editing commands:
# i - Enter insert mode
# ESC - Exit insert mode
# :wq - Save and quit
# :q - Quit (if no changes)
# :q! - Force quit without saving

3.4 cp: Copy Files and Directories

# Copy a file
cp source.txt destination/

# Copy a directory recursively
cp -r source_dir/ destination/

3.5 mv: Move or Rename

# Move a file
mv file.txt new_location/

# Rename a file
mv old_name.txt new_name.txt

3.6 rm: Remove Files and Directories

# Remove a file
rm unwanted_file.txt

# Remove a directory recursively and forcefully
rm -rf unwanted_directory/

# Common options:
# -r: Recursive removal
# -f: Force removal
# -v: Verbose output
# -i: Interactive prompt

3.7 chmod: Change File Permissions

# Change permissions using numeric notation
chmod 755 script.sh

# Change permissions recursively
chmod -R 644 directory/

# Permission values:
# r (read) = 4
# w (write) = 2
# x (execute) = 1
# Example: r-x = 5 (4+1)

3.8 tar: Archive and Compress

# Create a tar archive
tar -cvf archive.tar source_dir/

# Extract a tar archive
tar -xvf archive.tar

# Create a compressed tar archive
tar -zcvf archive.tar.gz source_dir/

# Extract a compressed tar archive
tar -zxvf archive.tar.gz

# Common options:
# c: Create archive
# x: Extract archive
# z: Use gzip compression
# v: Verbose output
# f: Specify filename

4. File Viewing and Searching

4.1 cat: Display File Contents

cat document.txt

4.2 head: View Beginning of File

# Show first 5 lines
head -5 document.txt

4.3 sed: Extract Specific Lines

# Show lines 5 through 10
sed -n '5,10p' document.txt

4.4 tail: View End of File

# Show last 5 lines
tail -5 document.txt

# Follow file changes in real-time
tail -f logfile.log

4.5 more: View File Page by Page

# View file with pagination
more document.txt

# Start from line 20
more +20 document.txt

4.6 less: Advanced File Viewer

# View file with navigation
less document.txt

4.7 grep: Search Text Patterns

# Basic search
grep "search_term" file.txt

# Case-insensitive search
grep -i "term" file.txt

# Show line numbers
grep -n "term" file.txt

# Search in multiple files
grep "term" *.txt

# Search with color highlighting
grep --color=auto "term" file.txt

4.8 find: Search for Files

# Find file by name
find /usr/local -name "TargetFile"

# Find files with pattern
find /usr/local -name "test*"

4.9 |: Pipe Operator

# Chain commands: find version in last 200 lines
tail -200 log.txt | grep "version"

4.10 Redirection Operators

# Overwrite file
command > output.txt

# Append to file
command >> output.txt

# Force overwrite
command >! output.txt

# Input from file
command < input.txt

5. User and Group Management

5.1 User Management

# Create a new user
useradd newuser

# Set user password
passwd newuser

# Delete a user
userdel olduser

# Delete user with home directory
userdel -r olduser

# View all users
cat /etc/passwd

5.2 Group Management

# Create a new group
groupadd developers

# Delete a group
groupdel developers

# Add user to group
gpasswd -a username developers

# Remove user from group
gpasswd -d username developers

# Check user's groups
groups username

# View all groups
cat /etc/group

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.