Creating, Copying, Moving, and Deleting Files
Learn the everyday commands for managing files and folders: touch, mkdir, cp, mv and rm, plus wildcards for working with many files at once.
Before you start
Make a practice folder so nothing important is at risk, and move into it:
mkdir ~/practice
cd ~/practice
mkdir is explained below. ~/practice is a path that starts in your home directory. All the commands in this lesson take arguments that are file or folder names, written after the command and separated by spaces.
Creating files: touch
touch notes.txt
touch a.txt b.txt c.txt
touch creates an empty file if the name does not exist yet. You can give it several names at once. If the file already exists, touch leaves the contents alone and only updates the "last modified" time.
Creating folders: mkdir
mkdir projects
mkdir -p projects/web/css
mkdir makes a directory. The -p option means "create the parent folders too". Without it, mkdir projects/web/css fails if projects/web does not exist yet. With -p all the missing levels are created in one go.
Copying: cp
cp copies a file. The pattern is always cp source destination.
cp notes.txt backup.txt
cp notes.txt projects/
cp -r projects projects-backup
- The first command copies
notes.txtto a new file calledbackup.txt. - The second copies
notes.txtinto the folderprojects. A destination that ends in/is treated as a folder. - The third copies a whole folder. The
-roption means recursive: copy the folder and everything inside it. Without-r,cprefuses to copy a folder.
After a copy, the original is still there. If the destination file already exists, cp replaces it without asking. Add -i to be asked first.
Moving and renaming: mv
mv notes.txt ideas.txt
mv ideas.txt projects/
mv projects/ideas.txt .
- The first command renames
notes.txttoideas.txt. Linux has no separate rename command: moving a file to a new name in the same place is renaming. - The second moves
ideas.txtinto theprojectsfolder. - The third moves it back to the current folder. A single dot
.means "here".
Unlike cp, mv does not leave the original behind. It also works on folders without -r. Like cp, it overwrites an existing destination without asking unless you add -i.
Deleting: rm and rmdir
rm ideas.txt
rm -i important.txt
rm -r old-project
rm -rf old-project
rmdir empty-folder
rmdeletes a file.-iasks for confirmation before each deletion.-rdeletes a folder and everything inside it.-fmeans force: never ask, and ignore files that do not exist. Combined as-rfit removes a whole folder silently.rmdirdeletes a folder only if it is empty, which makes it safer.
rm are gone for good. Be especially careful with rm -rf, and never run a delete command you copied from the internet without understanding it.Wildcards: matching many files
A wildcard is a special character that stands for other characters in a file name. The shell expands the pattern into the list of matching names before it runs the command.
*matches any number of characters, including none.*.txtmeans every file ending in.txt.?matches exactly one character.file?.txtmatchesfile1.txtandfile2.txtbut notfile10.txt.[abc]matches one character from the list.report[123].pdfmatchesreport1.pdf,report2.pdfandreport3.pdf.
ls *.txt
cp *.jpg photos/
mv report-?.pdf archive/
rm *.tmp
The safe way to use wildcards with rm
Before you delete with a pattern, run ls with the exact same pattern. You will see exactly what the shell is about to pass to rm.
ls *.tmp
rm *.tmp
If the list from ls is not what you expected, change the pattern before you delete anything.
Brace expansion and a full example
Curly braces let you write several names at once. {docs,images} expands into two separate words.
mkdir -p ~/demo/{docs,images}
cd ~/demo
touch docs/readme.txt images/logo.png
cp docs/readme.txt docs/readme-copy.txt
mv docs/readme-copy.txt docs/readme-old.txt
ls -R
ls -R lists the current folder and all folders inside it, so you can check the result.
Quick reference
touch: create an empty filemkdir: create a folder (-pfor nested folders)cp: copy (-rfor folders,-ito confirm overwrites)mv: move or renamerm: delete (-rfor folders,-ito confirm,-fto force)rmdir: delete an empty folder