Linux File System Operations and Directory Management
Linux Directory Structure Overview
The Linux file system follows a hierarchical directory structure with standardized locations for different types of files and system components.
Key System Directories
- /bin: Contains essential binary executables and system commands
- /boot: Stores boot loader files and kernel images
- /dev: Device files representing hardware components
- /etc: System configuration files and scripts
- /home: User home directories with individual workspace
- /lib: Shared library files required by system binaries
- /media: Mount point for removable media devices
- /mnt: Temporary mount point for file systems
- /opt: Optional software packages and applications
- /proc 2. /root: Home directory for the root user 3. /sbin: System administration binaries 4. /tmp: Temporary files cleared on reboot 5. /usr: User applications and read-only data 6. /var: Variable data including logs and spool files
File and Directory Operations
Navigation Commands
ls - List directory contents:
ls -la # Show all files including hidden with details
ls -d */ # Display only directories
cd - Change directory:
cd /absolute/path # Absolute path navigation
cd relative/path # Relative path navigation
cd ~ # Return to home directory
cd .. # Move to parent directory
pwd - Print working directory:
pwd -P # Show physical path without symbolic links
Directory Management
mkdir - Create directories:
mkdir project_folder
mkdir -p nested/folder/structure # Create parent directories
mkdir -m 755 secure_dir # Set specific permissions
rmdir - Remove empty directories:
rmdir empty_folder
rmdir -p level1/level2/level3 # Remove directory hierarchy
File Operations
cp - Copy files and directories:
cp source.txt destination.txt
cp -r source_dir/ target_dir/ # Recursive directory copy
cp -a original backup # Preserve all attributes
mv - Move or rename files:
mv oldname.txt newname.txt
mv file.txt target_directory/
mv -i existing newfile # Interactive overwrite
rm - Remove files and directories:
rm unwanted_file.txt
rm -r directory_to_delete # Recursive removal
rm -f protected_file # Force removal
Practical File Management Exercises
Directory Creation and Deletion
# Create personal workspace
mkdir /home/datawhale/username
cd /home/datawhale/username
# Create nested directory structure
mkdir datawhale_project
cd datawhale_project
# Create and remove test file
touch sample_data.txt
rm sample_data.txt
# Clean up directories
cd ..
rm -r datawhale_project
rm -r username
File Download and Inspection
# Create project structure
mkdir -p /home/datawhale/user_workspace/project_data
# Download dataset
wget -P /home/datawhale/user_workspace/project_data https://mirror.coggle.club/dataset/affairs.txt
# Examine file contents
head affairs.txt # First 10 lines
tail affairs.txt # Last 10 lines
cat affairs.txt # Entire file content
Python Environment Setup
# Launch Python environment
ipython
# Import and examine data
import pandas as pd
dataset = pd.read_csv('affairs.txt')
print(dataset.head())
Text Editing with Nano and Vim
Nano Editor
# Create Python script with Nano
nano hello_world.py
# Add script content:
#!/usr/bin/env python3
print('Hello World!')
# Save: Ctrl+O, Exit: Ctrl+X
Vim Editor
# Create script with Vim
vim greeting_script.py
# Insert mode: press 'i'
# Add content:
#!/usr/bin/env python3
print('Hello World!')
# Save and exit: Esc, :wq
Make Script Executable
chmod +x hello_world.py
./hello_world.py