Ravindra BagaleCourses & study guides

5. Linux Basic Commands

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

Many students don't check the path before running rm -rf – and then there's trouble. First look at the same path with ls, then write rm. There's no Recycle Bin on a server; once it's gone, it's gone. For important files use rm -i – it asks before deleting.

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.