5.3 Working with Files and Directories
| Command | Purpose | Example |
|---|---|---|
mkdir |
Make directory | mkdir project, mkdir -p a/b/c (create parents) |
touch |
Create empty file / update timestamp | touch index.html |
cp |
Copy | cp a.txt b.txt, cp -r site/ backup/ |
mv |
Move or rename | mv old.txt new.txt, mv file.txt /tmp/ |
rm |
Remove file | rm file.txt, rm -i *.log (ask first) |
rm -r |
Remove directory recursively | rm -r olddir |
rmdir |
Remove empty directory | rmdir emptydir |
ln -s |
Create symbolic link (shortcut) | ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled/ |
file |
Show file type | file /bin/ls |
stat |
Detailed file info | stat index.html |
mkdir -p ~/labs/site/css
cd ~/labs/site
touch index.html css/style.css
cp index.html index.bak
mv index.bak old-index.html
ls -R ~/labs
rm old-index.html
rm is permanent
There is no Recycle Bin on a Linux server. rm -rf /some/path deletes instantly and forever. Never run rm -rf with a variable that might be empty, and double-check the path before pressing Enter. Never run sudo rm -rf /.
Ravindra Bagale's Tip
rm -rf chalavnyapurvi khup students path check karat nahit – aani mag rada. Aadhi tech path ls ne bagha, mag rm lihaa. Server var Recycle Bin nahi, ekda gela te gela. Mahatvachya files sathi rm -i vapra – to vicharun delete karto.
Practice task
Create ~/labs/site/{css,js,img}, add index.html and css/style.css, copy the whole site to site-backup, rename it to site-old and finally delete it.