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 projectsprojects:appprojects/app:configmkdir namemakes one folder. It fails if the folder above it does not exist yet.mkdir -p a/b/cmakes 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/appWriting 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.txthello devopssecond line| Symbol | Meaning | How dangerous |
|---|---|---|
> | Write to the file and replace everything that was in it | High: one wrong file name wipes a config |
>> | Add to the end of the file | Low |
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/hostnameFor 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| Command | What happens |
|---|---|
cp a b | Copy 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 dir2 | Copy a whole folder and everything inside it |
cp -p a b | Keep the original permissions and dates |
cp -i a b | Ask 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.bakMoving 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 projectsLinux 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| Command | What happens |
|---|---|
rm file | Delete a file |
rm -r folder | Delete a folder and everything inside it |
rm -f file | Force: no error if the file is missing, no questions |
rmdir folder | Delete 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.
| Pattern | Matches | Think of it as |
|---|---|---|
*.log | Every file ending in .log | * = "anything" |
app-?.conf | app-1.conf, app-a.conf (exactly one character) | ? = "one character" |
report-[0-9].txt | report-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- Make a folder for the new version.
- Write its settings file.
- Copy the settings into the place the app reads from.
- 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 -rfwith an empty variable. In a script,rm -rf "$DIR"/with an emptyDIRbecomesrm -rf /, which tries to delete the whole system. Always check variables first.- Using
cpon 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.
- Make a folder called
projectsin your home folder. - Make a file
projects/hello.txtthat contains exactlyhello devops. - Copy it to
projects/hello-backup.txt.
Then try on your own: rename hello-backup.txt to hello.bak, then delete it.
Cheat sheet
| Command | What it does |
|---|---|
mkdir -p a/b | Make folders, including missing parents |
touch f | Make an empty file |
echo "x" > f | Replace the file's content with text |
echo "x" >> f | Add text to the end of a file |
cp -r a b | Copy a file or folder |
mv a b | Move or rename |
rm -r d | Delete a folder and everything in it |
Key takeaways
mkdirmakes a folder andtouchmakes a file.cpcopies,mvmoves or renames, andrmdeletes.>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
-rto copy or delete a whole folder.