TutsFx logo

Linux Command Line Basics

Creating Files and Directories

2 min read Last updated 45 minutes ago

Now you can move around — let's start creating things. These four commands cover most of daily file management.

Creating directories with mkdir

bash
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):

bash
touch notes.txt

Copying with cp

bash
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:

bash
mv notes.txt notes2.txt     # rename
mv notes2.txt projects/     # move into a directory

Deleting with rm

bash
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

  1. mkdir -p sandbox/a sandbox/b
  2. touch sandbox/a/first.txt
  3. cp sandbox/a/first.txt sandbox/b/second.txt
  4. mv sandbox/b/second.txt sandbox/a/renamed.txt
  5. ls -R sandbox — see the whole tree at once
Share:

We value your privacy

We use cookies to enhance your browsing experience, serve personalized ads, and analyze traffic. By clicking "Accept", you consent to our use of cookies. Read our Privacy Policy to learn more.