Skip to content
$ lynuxpro
← Linux

LESSON 3 OF 26 · BEGINNER · 16 MIN

Creating, copying and moving files

In one minute

  • A small set of commands to make, copy, move, rename and delete files and folders.
  • DevOps work is full of small file jobs: make a folder for a new app, back up a config before you change it, put a new release in place, clean up old logs.
  • Each job has one short command: mkdir (make folder), touch (make file), cp (copy), mv (move or rename), rm (remove). Learn these well and you can manage files on any server.
  • 👉 Setting up a new app, taking a backup before a change, deploying a release, and cleaning up when the disk is full.

Big warning first: Linux has no recycle bin and no undo. A deleted or overwritten file is gone. Look before you delete, and back up before you edit.

Making folders: mkdir

mkdir makes a new folder. It stands for make directory.

Apps, releases and projects each need their own folder to stay organised.

Give it a name. Add -p to make a whole chain of folders in one go.

mkdir projectsmkdir -p projects/app/configls -R projects
SHELLbashutf-8 · 3L
projects:appprojects/app:config
OUTPUTterminalutf-8 · 5L
  • mkdir name makes one folder. It fails if the folder above it does not exist yet.
  • mkdir -p a/b/c makes every missing folder in the chain. It also does not complain if the folder already exists. That makes it safe in scripts that might run twice.

Remember it: -p = make the parents too.

Making empty files: touch

touch makes an empty file.

Handy when you need a file to exist before you fill it, such as a placeholder or a log file.

Give it a file name. If the file already exists, touch leaves the content alone and only updates its "last changed" time.

touch projects/app/README.mdls -l projects/app
SHELLbashutf-8 · 2L

Writing text into files: echo, > and >>

echo prints text. The arrows > and >> send that text into a file instead of onto the screen.

It is the quickest way to write a small file or add a line to one, by hand or in a script.

echo "hello devops" > notes.txtecho "second line" >> notes.txtcat notes.txt
SHELLbashutf-8 · 3L
hello devopssecond line
OUTPUTterminalutf-8 · 2L
SymbolMeaningHow dangerous
>Write to the file and replace everything that was in itHigh: one wrong file name wipes a config
>>Add to the end of the fileLow

Remember it: One arrow > = wipe and write. Two arrows >> = add on.

A classic outage: someone meant to type >>, typed >, and emptied an important file. Slow down every time you type >.

Reading files: cat

cat prints a whole file on the screen.

To quickly check what is inside a small file.

cat notes.txtcat /etc/hostname
SHELLbashutf-8 · 2L

For big files, cat floods the screen. The next lesson shows less, head and tail for those.

Copying: cp

cp makes a copy of a file or folder.

Mostly for backups: copy a file before you change it, so you can go back if something breaks.

cp from to. Add -r for folders.

cp notes.txt notes.bakcp -r projects projects-backupls
SHELLbashutf-8 · 3L
CommandWhat happens
cp a bCopy file a to b. If b exists, it is replaced without asking
cp a folder/Copy a into folder, keeping the same name
cp -r dir1 dir2Copy a whole folder and everything inside it
cp -p a bKeep the original permissions and dates
cp -i a bAsk before replacing a file

Remember it: -r = recursive = "and everything inside". -i = interactive = "ask me first".

Good habit: before you edit any config on a server, take a backup copy:

cp /etc/hosts /tmp/hosts.bak
SHELLbashutf-8 · 1L

Moving and renaming: mv

mv moves a file or folder to a new place, or gives it a new name.

To tidy up, put a file where it belongs, or rename it.

👉 Putting a new release in place, or fixing a badly named file.

mv notes.bak backup.txtmv backup.txt projects/ls projects
SHELLbashutf-8 · 3L

Linux has no separate "rename" command. Renaming is just moving a file to a new name in the same folder. mv also moves whole folders without needing -r.

Remember it: mv = move = rename. Same thing.

Deleting: rm and rmdir

rm removes (deletes) files. rmdir removes empty folders.

To clean up old logs, old releases and test files, so the disk does not fill up.

rm projects/backup.txtrmdir projects/app/configrm -r projects-backup
SHELLbashutf-8 · 3L
CommandWhat happens
rm fileDelete a file
rm -r folderDelete a folder and everything inside it
rm -f fileForce: no error if the file is missing, no questions
rmdir folderDelete a folder only if it is empty (the safe choice)

Safety rule: before any rm -r, run ls on exactly the same path. See what you are about to delete, then delete it.

Wildcards: patterns that match many files

A wildcard is a pattern, such as *.log, that matches many file names at once.

So you can act on 50 log files with one command instead of typing 50 names.

The shell swaps the pattern for the matching file names before the command runs.

PatternMatchesThink of it as
*.logEvery file ending in .log* = "anything"
app-?.confapp-1.conf, app-a.conf (exactly one character)? = "one character"
report-[0-9].txtreport-0.txt to report-9.txt[ ] = "one of these"

A wildcard in the wrong folder plus rm is how people delete the wrong things. Check first with ls *.log, then run rm *.log.

Real-world scenario: getting a release folder ready

You are putting version 1.4 of an app in place by hand:

mkdir -p /tmp/app/releases/1.4echo "version=1.4" > /tmp/app/releases/1.4/app.propertiescp /tmp/app/releases/1.4/app.properties /tmp/app/current.propertiesls -R /tmp/app
SHELLbashutf-8 · 4L
  1. Make a folder for the new version.
  2. Write its settings file.
  3. Copy the settings into the place the app reads from.
  4. Check the result.

Deployment tools like Ansible, Helm and Argo CD do exactly these steps for you, automatically.

Common mistakes

  • Typing > instead of >>, which wipes the file.
  • rm -rf with an empty variable. In a script, rm -rf "$DIR"/ with an empty DIR becomes rm -rf /, which tries to delete the whole system. Always check variables first.
  • Using cp on a folder without -r. You get "omitting directory" and nothing is copied.

Practice: live lab

Open the lab on the right and do the tasks. Press Check after each one.

  1. Make a folder called projects in your home folder.
  2. Make a file projects/hello.txt that contains exactly hello devops.
  3. Copy it to projects/hello-backup.txt.

Then try on your own: rename hello-backup.txt to hello.bak, then delete it.

Cheat sheet

CommandWhat it does
mkdir -p a/bMake folders, including missing parents
touch fMake an empty file
echo "x" > fReplace the file's content with text
echo "x" >> fAdd text to the end of a file
cp -r a bCopy a file or folder
mv a bMove or rename
rm -r dDelete a folder and everything in it

Key takeaways

  • mkdir makes a folder and touch makes a file.
  • cp copies, mv moves or renames, and rm deletes.
  • > replaces what is in a file. >> adds to the end.
  • Linux has no recycle bin, so back up before you edit and look before you delete.
  • Add -r to copy or delete a whole folder.