Linux File Operations
Essential commands for managing files and directories in Linux, including file transfers, permissions, and common operations.
File Transfer (SCP)
SCP (Secure Copy Protocol) allows secure file transfer over SSH.
Copy Files from Server to Local
# Copy single file
scp user@192.168.1.55:/remote/path/file.txt /local/path/
# Copy multiple files
scp user@192.168.1.55:/remote/path/*.txt /local/path/
# Copy directory recursively
scp -r user@192.168.1.55:/remote/path/folder /local/path/
# Preserve file attributes (timestamps, permissions)
scp -p user@192.168.1.55:/remote/file.txt /local/path/Copy Files from Local to Server
# Copy single file
scp /local/path/file.txt user@192.168.1.55:/remote/path/
# Copy multiple files
scp /local/path/*.txt user@192.168.1.55:/remote/path/
# Copy directory recursively
scp -r /local/path/folder user@192.168.1.55:/remote/path/
# From README example
scp /local/folder/*.* user@192.168.1.55:/folder/SCP Options
# Use specific port
scp -P 2222 file.txt user@host:/path/
# Use specific SSH key
scp -i ~/.ssh/id_rsa file.txt user@host:/path/
# Limit bandwidth (in Kbit/s)
scp -l 1000 file.txt user@host:/path/
# Verbose output
scp -v file.txt user@host:/path/
# Compress during transfer
scp -C largefile.tar user@host:/path/
# Quiet mode (no progress bar)
scp -q file.txt user@host:/path/SCP Examples
# Backup directory to remote server
scp -r ~/Documents/project user@backup.server.com:/backups/
# Download log files
scp user@webserver.com:/var/log/nginx/*.log ./logs/
# Transfer with compression
scp -C database-dump.sql user@server.com:/backups/
# Copy between remote servers (from local machine)
scp user1@server1:/path/file.txt user2@server2:/path/File Permissions (chmod)
Permission Basics
Linux permissions have three groups:
- Owner (u): The file owner
- Group (g): Users in the file's group
- Others (o): Everyone else
Each group has three permission types:
- Read (r): 4
- Write (w): 2
- Execute (x): 1
Numeric (Octal) Mode
# Common permissions
chmod 644 file.txt # rw-r--r-- (owner: rw, group: r, others: r)
chmod 755 script.sh # rwxr-xr-x (owner: rwx, group: rx, others: rx)
chmod 700 private # rwx------ (owner: rwx, group: none, others: none)
chmod 600 key.pem # rw------- (owner: rw, group: none, others: none)
chmod 777 temp # rwxrwxrwx (all permissions for everyone)Symbolic Mode
# Add permissions
chmod u+x script.sh # Add execute for owner
chmod g+w file.txt # Add write for group
chmod o+r file.txt # Add read for others
chmod a+x script.sh # Add execute for all (all = ugo)
# Remove permissions
chmod u-x script.sh # Remove execute from owner
chmod g-w file.txt # Remove write from group
chmod o-r file.txt # Remove read from others
# Set exact permissions
chmod u=rwx file.txt # Set owner to rwx
chmod g=r file.txt # Set group to r only
chmod o= file.txt # Remove all permissions for othersRecursive Permissions
# Add read/write for all groups recursively (from README)
sudo chmod -R a+rw /path/to/folder
# Common recursive examples
sudo chmod -R 755 /var/www/html # Web server directories
sudo chmod -R 644 /var/www/html/*.html # Web files
sudo chmod -R u+rwX /data # Add rwX for owner (X = execute on dirs only)
# Set directory and file permissions separately
find /path -type d -exec chmod 755 {} \; # Directories
find /path -type f -exec chmod 644 {} \; # FilesPermission Examples
# Make script executable
chmod +x script.sh
# Secure SSH key
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
# Web server permissions
sudo chmod 755 /var/www/html
sudo chmod 644 /var/www/html/index.html
# Shared folder (read/write for owner and group)
chmod 770 shared-folder
# Public read-only
chmod 444 public-document.txtFile Ownership (chown)
Change Owner
# Change owner
sudo chown user file.txt
# Change owner and group
sudo chown user:group file.txt
# Change owner recursively
sudo chown -R user:group /path/to/directory
# Change only group (alternative to chgrp)
sudo chown :group file.txtCommon Examples
# Change web server ownership
sudo chown -R www-data:www-data /var/www/html
# Change to current user
sudo chown $USER:$USER file.txt
# Change Docker files ownership
sudo chown -R $USER:$USER ~/.dockerFinding Files
Using find
# Find by name (case-insensitive)
find ./ -iname "*test*"
# Find files only
find ./ -type f -name "*.txt"
# Find directories only
find ./ -type d -name "backup"
# Find and delete
find ./ -name "*.tmp" -delete
# Find files modified in last 7 days
find ./ -mtime -7
# Find files larger than 100MB
find ./ -size +100M
# Find and execute command
find ./ -name "*.log" -exec gzip {} \;Using locate
# Find file by name (uses database)
locate filename
# Update locate database
sudo updatedb
# Case-insensitive search
locate -i filename
# Limit results
locate -l 10 filenameFile Compression
tar
# Create tar archive
tar -cvf archive.tar /path/to/files
# Create compressed tar.gz
tar -czvf archive.tar.gz /path/to/files
# Create compressed tar.bz2
tar -cjvf archive.tar.bz2 /path/to/files
# Extract tar
tar -xvf archive.tar
# Extract tar.gz
tar -xzvf archive.tar.gz
# Extract to specific directory
tar -xzvf archive.tar.gz -C /destination/path
# List contents without extracting
tar -tzvf archive.tar.gzgzip/gunzip
# Compress file
gzip file.txt # Creates file.txt.gz
# Decompress file
gunzip file.txt.gz
# Keep original file
gzip -k file.txt
# Compress recursively
gzip -r /path/to/directoryzip/unzip
# Create zip archive
zip archive.zip file1.txt file2.txt
# Zip directory recursively
zip -r archive.zip /path/to/directory
# Extract zip
unzip archive.zip
# Extract to specific directory
unzip archive.zip -d /destination/path
# List zip contents
unzip -l archive.zipFile Comparison
diff
# Compare files
diff file1.txt file2.txt
# Side-by-side comparison
diff -y file1.txt file2.txt
# Ignore whitespace
diff -w file1.txt file2.txt
# Unified format (like git diff)
diff -u file1.txt file2.txt
# Compare directories
diff -r dir1/ dir2/Disk Usage
du (Disk Usage)
# Disk usage of directory
du -sh /path/to/directory
# Disk usage with details
du -h /path/to/directory
# Sort by size
du -h /path | sort -rh | head -10
# All files in directory
du -ah /path/to/directory
# Disk usage summary
du -sh /*df (Disk Free)
# Disk space usage
df -h
# Specific filesystem
df -h /dev/sda1
# Show inodes
df -iFile Watching
tail
# Watch file in real-time
tail -f /var/log/syslog
# Last 100 lines and follow
tail -n 100 -f /var/log/nginx/access.log
# Multiple files
tail -f /var/log/nginx/*.log
# From README example
tail -f /var/log/syslogwatch
# Watch command output (updates every 2 seconds)
watch df -h
# Update every 1 second
watch -n 1 'ls -lh'
# Highlight differences
watch -d 'ps aux | grep nginx'Symlinks
Create Symlinks
# Create symbolic link
ln -s /path/to/original /path/to/link
# Create hard link
ln /path/to/original /path/to/hardlink
# Force overwrite existing link
ln -sf /path/to/original /path/to/linkCommon Symlink Uses
# Link configuration
sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/
# Link binary to PATH
sudo ln -s /opt/myapp/bin/myapp /usr/local/bin/myapp
# Link current version
ln -s /opt/myapp-1.0.0 /opt/myappAdvanced Operations
File Attributes
# Make file immutable (cannot be deleted or modified)
sudo chattr +i file.txt
# Remove immutable flag
sudo chattr -i file.txt
# List file attributes
lsattr file.txt
# Append-only (can only be written to)
sudo chattr +a logfile.logBatch Operations
# Rename multiple files
for file in *.txt; do mv "$file" "${file%.txt}.bak"; done
# Convert files to lowercase
for file in *; do mv "$file" "$(echo $file | tr '[:upper:]' '[:lower:]')"; done
# Add prefix to files
for file in *.jpg; do mv "$file" "photo_$file"; doneSafe File Deletion
# Move to trash instead of deleting
trash file.txt # Requires trash-cli
# Install trash-cli
sudo apt install trash-cli # Debian/Ubuntu
# Restore from trash
trash-restore
# Empty trash
trash-emptyCedilla (ç) with US International Keyboard
On Ubuntu Desktop with the English (US, intl., with dead keys) keyboard layout, typing ' + c produces ć instead of ç. This happens because the default Compose sequences prioritize the acute accent.
To fix this, create or edit the ~/.XCompose file to override the default behavior.
Configure ~/.XCompose
# Create or edit ~/.XCompose
nano ~/.XComposeAdd the following content:
include "%L"
<dead_acute> <c> : "ç" ccedilla
<dead_acute> <C> : "Ç" CcedillaExplanation
include "%L"loads the default system Compose rules for your locale- The two lines override the
' + cand' + Ccombinations to produceçandÇinstead ofćandĆ
Apply the Changes
Log out and log back in, or restart the application where you want to use the cedilla. Some applications (like terminals and text editors) pick up the change immediately after restart.
GTK Applications
For GTK-based applications (GNOME Terminal, Nautilus, etc.), you may also need to set the input module. Add this to your ~/.profile or ~/.bashrc:
export GTK_IM_MODULE=cedillaThen log out and log back in.
Verify
After restarting, type ' + c — it should now produce ç instead of ć.
Troubleshooting
Permission Denied
# Check current permissions
ls -la file.txt
# Check file owner
stat file.txt
# Fix ownership
sudo chown $USER:$USER file.txt
# Add read permission
chmod +r file.txtFile in Use
# Find processes using file
lsof /path/to/file
# Find processes using directory
lsof +D /path/to/directory
# Kill processes using file
fuser -k /path/to/fileSee Also
- Cross-Platform File Commands - File operations across OSes
- Linux System Monitoring - Disk usage monitoring
- SSH Configuration - SSH keys for SCP