Prerequisites
Before creating or deleting files, make sure you have:
- SSH access to your VPS
- Write permissions in the target directory
⚠️ Deleting files is permanent on Linux. Always double-check before running rm commands.
Create Files - Multiple Methods
Connect to your VPS:
ssh hxroot@YOUR_SERVER_IP -p 22
Method 1: Using touch (Empty File)
touch filename.txt
Create multiple files:
touch file1.txt file2.txt file3.txt
Method 2: Using echo (File with Content)
echo "Hello World" > filename.txt
Append to file:
echo "Another line" >> filename.txt
Method 3: Using cat (Create and Type Content)
cat > filename.txt
Type your content, then press Ctrl+D to save.
Method 4: Using nano/vim (Interactive Editor)
nano filename.txt
Type content, then Ctrl+O, Ctrl+X to save.
Method 5: Using printf (Formatted Content)
printf "Line 1
Line 2
Line 3
" > filename.txt
Method 6: Using truncate (Specific Size)
Create 1MB empty file:
truncate -s 1M filename.img
Delete Files
Method 1: Using rm (Remove File)
rm filename.txt
Remove multiple files:
rm file1.txt file2.txt file3.txt
Remove with confirmation prompt:
rm -i filename.txt
Force remove (no confirmation):
rm -f filename.txt
Verbose output (shows what was deleted):
rm -v filename.txt
Method 2: Using unlink (Single File)
unlink filename.txt
Method 3: Delete Files with Pattern
Delete all .txt files:
rm *.txt
Delete files starting with "backup":
rm backup*
Delete files ending with ".log":
rm *.log
Delete files with wildcard (careful!):
rm * # Deletes ALL files in current directory
Safe Delete Practices
List files before deleting:
ls *.txt
rm *.txt
Use interactive mode:
rm -i *.txt
Create trash function (safer):
alias trash='mv --backup -t ~/.local/share/Trash '
Then use:
trash filename.txt
Delete Files by Age
Delete files older than 30 days:
find /path -type f -mtime +30 -delete
Delete files older than 7 days with confirmation:
find /path -type f -mtime +7 -exec rm -i {} ;
Delete Files by Size
Delete empty files:
find /path -type f -size 0 -delete
Delete files larger than 100MB:
find /path -type f -size +100M -delete
Recover Deleted Files
Important: Deleted files are generally NOT recoverable on Linux without special tools.
If you accidentally deleted a file:
# Stop using the system immediately
# Try using testdisk or photorec
apt install testdisk -y
photorec /dev/sda1
Script Examples
Create numbered files:
#!/bin/bash
for i in {1..10}; do
touch "file_$i.txt"
echo "Created file_$i.txt"
done
Delete files with .tmp extension:
find /tmp -type f -name "*.tmp" -mtime +1 -delete
Check File Existence Before Delete
#!/bin/bash
if [ -f "filename.txt" ]; then
rm filename.txt
echo "File deleted"
else
echo "File not found"
fi
✅ You can now create and delete files on your Hostxpeed VPS using multiple methods.