Files & Directories

Linux · Lesson 6 of 32 7 min read Updated

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.

Terminal
touch notes/shopping.txt
ls -l notes/shopping.txt
mkdir -pv college/sem3/dbms
Output
-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.

Terminal
cp -v notes/todo.txt notes/todo-backup.txt
cp -v orders.csv Documents/
cp -rv projects/shop-site projects/shop-site-v2
Output
'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.

Terminal
mv -v notes/todo-backup.txt Documents/
mv -v projects/shop-site-v2 projects/sweets-site
mv -i Documents/orders.csv Documents/orders-oct.csv
Output
renamed '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.

Terminal
rm -v Documents/todo-backup.txt
rmdir college
rm -rv college
Output
removed '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:

PatternMatchesExample
*Any number of characters (including none)*.csv matches every CSV file
?Exactly one characterfile?.txt matches file1.txt, not file10.txt
[abc]One character from the setreport-[12].pdf
[0-9]One character from a rangesem[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:

Terminal
echo *.csv
echo notes/*.txt
echo file{1..3}.txt
mkdir -p shop/{css,js,images}
Output
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.

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.

Terminal
ln -s /home/priya/projects/shop-site ~/site
ls -l ~/site
Output
lrwxrwxrwx 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 -r when copying a folder. cp skips it with "omitting directory". Use cp -r (or cp -a).
  • Overwriting by accident. cp and mv replace existing files silently. Add -i or -n when it matters.
  • Spaces inside rm arguments. rm -r my folder deletes my and folder, not my folder. Quote the name.
  • Expecting a recycle bin. Deleted files are gone. Back up first.

Key points

  • touch creates empty files; mkdir -p creates nested folders without errors.
  • cp copies (-r for folders, -a to keep permissions and times, -i to ask first).
  • mv moves and renames; rm deletes files, rm -r deletes folders, rmdir deletes only empty folders.
  • There is no undo: check wildcards with echo or ls before deleting.
  • *, ? and [...] match existing names; {a,b} and {1..5} build names.
  • ln -s creates 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
Terminal
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-backup
Output
contact.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?

2. How do you rename old.txt to new.txt?

3. What does cp notes backup print if notes is a directory?

4. Which command is the safest way to delete an empty folder called tmpwork?

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