Kodokon kodokon.com

Creating, copying, moving, deleting

Create folders and files, then copy, rename and delete them from the command line.

8 min · 3 questions

Open this lesson in Kodokon

Knowing how to move around is good; acting on files is better. Five commands cover 90% of what you will do: mkdir to create a folder, touch to create an empty file, cp to copy, mv to move or rename, and rm to delete. They all share the same shape: the name of the command, then whatever it acts on. Let us start with mkdir, which stands for make directory. You give it the name of the folder to create. By default it creates only one level: if you ask for mkdir src/pages while src does not exist, it refuses. The -p option (for parents) fixes that by creating every missing intermediate folder along the way.

BASH
mkdir my-project
cd my-project
mkdir -p src/pages
ls
Create a folder, step into it, then create a whole tree in a single command.

touch creates an empty file if it does not exist yet. You can create several at once by separating the names with spaces. It is handy for laying out a project structure before opening it in your editor.

BASH
touch index.html
touch notes.txt README.md
ls
One file, then two files created in a single command.

cp (copy) takes two arguments: the source, then the destination. To copy an entire folder with everything inside it, add the -r option (for recursive, meaning "and everything in there"). mv (move) works the same way, but moves instead of copying. One detail that often catches people out: renaming is moving. Moving notes.txt to notes-personal.txt inside the same folder is exactly the same thing as renaming it, and mv is indeed the command for the job.

BASH
cp index.html backup.html
mv notes.txt notes-personal.txt
mv notes-personal.txt src/
cp -r src copy-of-src
ls
Copy, rename, move into a subfolder, then copy an entire folder.

Knowledge check

Make sure you remember the key points of this lesson.

  1. Put these commands back in order to create the demo folder, move into it, then drop the file src/pages/index.html inside it.
    mkdir demo
    cd demo
    mkdir -p src/pages
    touch src/pages/index.html
  2. How do you rename the file draft.txt to report.txt?
    • rename draft.txt report.txt
    • cp draft.txt report.txt
    • mv draft.txt report.txt
  3. You delete a file with rm. Where do you find it if you change your mind?
    • In the system recycle bin
    • Nowhere: the deletion is permanent
    • In the parent folder, with a dot in front of its name