Bash Tutorial · Bash Basics · Lesson 5 of 30

Bash Files and Directories

Create, copy, move, rename and delete files and directories safely.

Create a free account to save this page, take notes and track your progress. Sign in

Creating

  • mkdir dir creates a directory; mkdir -p a/b/c creates parents as needed.
  • touch file creates an empty file (or updates its timestamp).

Copying, moving and deleting

cp, mv and rm
mkdir -p backup
echo "v1" > config.ini
cp config.ini backup/config.ini
mv config.ini settings.ini
ls
ls backup
rm -r backup
ls

Output

backup
settings.ini
config.ini
settings.ini

Run this on your computer — see “Get Started” for setup.

  • cp src dst copies; cp -r copies directories.
  • mv src dst moves or renames.
  • rm file deletes; rm -r dir deletes a directory tree. There is no recycle bin: deleted means gone.

Wildcards (globs)

The shell expands patterns before the command runs: * (anything), ? (one character), [abc] (one of).

Globbing
touch report-jan.csv report-feb.csv report-mar.csv summary.txt
echo *.csv
echo report-???.csv
echo report-[fm]*.csv

Output

report-feb.csv report-jan.csv report-mar.csv
report-feb.csv report-jan.csv report-mar.csv
report-feb.csv report-mar.csv

Run this on your computer — see “Get Started” for setup.

Test yourself

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

  1. rename old.txt new.txt
  2. cp old.txt new.txt
  3. mv old.txt new.txt
  4. rn old.txt new.txt
Show answer

Answer: mv old.txt new.txt

Was this page helpful?