Skip to content

Working with files

Terminal window
mkdir projects # make a folder
mkdir -p projects/a/b/c # make a whole chain of folders at once
touch notes.txt # make an empty file

touch updates a file’s date, and if the file doesn’t exist, it creates an empty one (this is a very popular use).

Terminal window
cat notes.txt # print the whole file
less notes.txt # scroll through a long file (q to quit)
head notes.txt # first 10 lines
tail notes.txt # last 10 lines
Terminal window
cp notes.txt backup.txt # copy a file
cp -r projects projects-backup # copy a folder (-r means "and everything inside")
mv notes.txt Documents/ # move a file into a folder
mv backup.txt old-notes.txt # rename a file

There’s no separate rename command. Renaming is just moving a file to a new name in the same place.

Terminal window
rm old-notes.txt # delete a file
rmdir empty-folder # delete an empty folder
rm -r projects-backup # delete a folder and everything in it

* matches any text. The shell replaces it with every matching file name before running the command:

Terminal window
ls *.png # every file ending in .png
cp *.jpg backup/ # copy every .jpg into backup/

This is called a glob.

Quick check: what does mv *.txt notes/ do?

It moves every file ending in .txt (in the folder you’re in) into the notes folder.