Linux Command Line Basics
Creating Files and Directories
Now you can move around — let's start creating things. These four commands cover most of daily file management.
Creating directories with mkdir
mkdir projects # create one directory
mkdir -p a/b/c # create nested directories in one go
The -p flag creates any missing parent directories and, unlike plain mkdir, does not error if the directory already exists.
Creating files with touch
touch creates an empty file (or updates the timestamp of an existing one):
touch notes.txt
Copying with cp
cp notes.txt backup.txt # copy a file
cp -r projects projects-backup # copy a directory (recursive)
`cp` does not copy directories without `-r`
Running cp olddir newdir on a directory fails with an error. Add -r to copy directories recursively.
Moving and renaming with mv
mv moves a file — and because moving a file into the same directory is the same as renaming it, mv is also how you rename:
mv notes.txt notes2.txt # rename
mv notes2.txt projects/ # move into a directory
Deleting with rm
rm notes.txt # delete a file
rm -r projects # delete a directory and everything inside it
`rm -rf` is permanent
There is no trash bin on the command line. rm deletes files permanently, and rm -rf deletes directories without asking. Triple-check the path before you press Enter.
Try it
mkdir -p sandbox/a sandbox/btouch sandbox/a/first.txtcp sandbox/a/first.txt sandbox/b/second.txtmv sandbox/b/second.txt sandbox/a/renamed.txtls -R sandbox— see the whole tree at once
Advertisement