Files & Directories
Now that you can move around, it is time to change things: make folders, create files, copy, rename and delete them. Linux does all of this with short commands: touch, mkdir, cp, mv and rm.
These commands are powerful and quiet. They usually print nothing when they succeed, and there is no recycle bin on the command line. This lesson shows each command, the options that make it safer, and the shell shortcuts (wildcards and braces) that let you work on many files at once.
Creating files and folders
touch creates an empty file if it does not exist. If the file already exists, touch just updates its modification time and leaves the contents alone. mkdir (make directory) creates folders.
touch notes/shopping.txt
ls -l notes/shopping.txt
mkdir -pv college/sem3/dbms-rw-rw-r-- 1 priya priya 0 Oct 30 09:20 notes/shopping.txt mkdir: created directory 'college' mkdir: created directory 'college/sem3' mkdir: created directory 'college/sem3/dbms'
The file has size 0: it exists but is empty. The -p option (parents) creates every missing folder along the path, and it does not complain if a folder already exists. Without -p, mkdir college/sem3/dbms fails when college does not exist yet. The -v option (verbose) makes the command report what it did; many file commands support it.
Copying with cp
cp source destination copies a file. If the destination is an existing directory, the copy goes inside it with the same name.
cp -v notes/todo.txt notes/todo-backup.txt
cp -v orders.csv Documents/
cp -rv projects/shop-site projects/shop-site-v2'notes/todo.txt' -> 'notes/todo-backup.txt' 'orders.csv' -> 'Documents/orders.csv' 'projects/shop-site' -> 'projects/shop-site-v2' 'projects/shop-site/images' -> 'projects/shop-site-v2/images' 'projects/shop-site/index.html' -> 'projects/shop-site-v2/index.html' 'projects/shop-site/style.css' -> 'projects/shop-site-v2/style.css'
To copy a directory you must add -r (recursive), which copies everything inside it. Without it, cp refuses with "-r not specified; omitting directory". Two more options are worth knowing: -i (interactive) asks before overwriting an existing file, and -a (archive) copies recursively while keeping permissions, owners and timestamps, which is what you want for backups.
Moving and renaming with mv
Linux has no separate "rename" command: renaming is just moving a file to a new name in the same folder. mv works on directories without any -r.
mv -v notes/todo-backup.txt Documents/
mv -v projects/shop-site-v2 projects/sweets-site
mv -i Documents/orders.csv Documents/orders-oct.csvrenamed 'notes/todo-backup.txt' -> 'Documents/todo-backup.txt' renamed 'projects/shop-site-v2' -> 'projects/sweets-site'
The first line moves a file into another folder, the second renames a folder, and the third renames a file with -i so that mv asks before replacing anything. Like cp, plain mv silently overwrites an existing destination file.
Deleting with rm and rmdir
rm removes files. rmdir removes only empty directories, which makes it a safe choice. To delete a directory with everything in it, use rm -r.
rm -v Documents/todo-backup.txt
rmdir college
rm -rv collegeremoved 'Documents/todo-backup.txt' rmdir: failed to remove 'college': Directory not empty removed directory 'college/sem3/dbms' removed directory 'college/sem3' removed directory 'college'
rm -i asks about every file, and rm -I asks once before deleting more than three files or before a recursive delete, which is a good balance. rm -f (force) never asks and ignores missing files; combined with -r it deletes whole trees silently.
Wildcards and brace expansion
The shell can expand patterns into lists of file names before the command runs. This is called globbing, and the patterns are wildcards:
| Pattern | Matches | Example |
|---|---|---|
* | Any number of characters (including none) | *.csv matches every CSV file |
? | Exactly one character | file?.txt matches file1.txt, not file10.txt |
[abc] | One character from the set | report-[12].pdf |
[0-9] | One character from a range | sem[1-4] |
Brace expansion is different: it builds names whether or not the files exist, which makes it perfect for creating things. echo lets you preview any pattern safely:
echo *.csv
echo notes/*.txt
echo file{1..3}.txt
mkdir -p shop/{css,js,images}orders.csv notes/shopping.txt notes/todo.txt file1.txt file2.txt file3.txt
The last line creates three folders in one go. Wildcards do not match hidden files (names starting with a dot) unless the pattern itself starts with a dot.
Advanced: links
A symbolic link (symlink) is a small file that points to another path, like a shortcut on Windows. ln -s target linkname creates one. Web servers use them a lot: nginx keeps site settings in sites-available and enables a site by linking it into sites-enabled.
ln -s /home/priya/projects/shop-site ~/site
ls -l ~/sitelrwxrwxrwx 1 priya priya 30 Oct 30 09:41 /home/priya/site -> /home/priya/projects/shop-site
Deleting the link with rm ~/site removes only the shortcut, never the target. If you delete the target, the link becomes "broken" and points to nothing. Without -s, ln makes a hard link: a second name for the very same file data, which only works on the same filesystem and not for directories.
Common mistakes
- Forgetting
-rwhen copying a folder.cpskips it with "omitting directory". Usecp -r(orcp -a). - Overwriting by accident.
cpandmvreplace existing files silently. Add-ior-nwhen it matters. - Spaces inside
rmarguments.rm -r my folderdeletesmyandfolder, notmy folder. Quote the name. - Expecting a recycle bin. Deleted files are gone. Back up first.
Key points
touchcreates empty files;mkdir -pcreates nested folders without errors.cpcopies (-rfor folders,-ato keep permissions and times,-ito ask first).mvmoves and renames;rmdeletes files,rm -rdeletes folders,rmdirdeletes only empty folders.- There is no undo: check wildcards with
echoorlsbefore deleting. *,?and[...]match existing names;{a,b}and{1..5}build names.ln -screates a symbolic link that points to another file or folder.
Exercise
In your home folder, build this structure with as few commands as you can: a folder practice containing css, js and images, and empty files index.html and about.html inside practice. Then copy the whole practice folder to practice-backup, rename about.html in the original to contact.html, and finally delete practice-backup safely.
Show solution
mkdir -p practice/{css,js,images}
touch practice/{index,about}.html
cp -r practice practice-backup
mv practice/about.html practice/contact.html
ls practice
rm -rI practice-backupcontact.html css images index.html js rm: remove 1 argument recursively? y
Brace expansion makes the three folders and two files in two commands. rm -rI asks once before the recursive delete, so you get one last chance to stop if you typed the wrong name.
Quick Quiz
1. Which command creates a/b/c even if a does not exist yet?
-p creates every missing parent directory along the path.
2. How do you rename old.txt to new.txt?
Renaming is moving a file to a new name, so you use mv.
3. What does cp notes backup print if notes is a directory?
cp needs -r (or -a) to copy directories.
4. Which command is the safest way to delete an empty folder called tmpwork?
rmdir refuses to delete a folder that still has files in it, so it cannot destroy work by accident.
Learn Linux with a mentor
Free lessons get you started. Our Ethical Hacking & Cyber Security training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
