16 min read
Linux commands for working with files
In one sentence: On a server, everything is done with a handful of commands: mkdir, cp, mv and rm to create, copy, move and delete; less, head and tail to read; grep and find to search, and nano to edit. And rm deletes forever: there's no trash.
What you will learn
- Create, copy, move, rename and delete files and folders, knowing that rm has no trash.
- Read a large file without getting lost, with less, head and tail, and follow one that grows with tail -f.
- Find files by name with find, and text inside them with grep.
- Edit a file with nano, and get out of vim if you land in it.
Before you start, read: 01-the-shell-commands-and-paths.md
The problem
Section titled “The problem”Imagine you look after an online shop’s website. One morning, the owner writes to you: “I’ve been told someone tried to steal our passwords last night. Can you look into it?” The only clue is on the server: its LogA text file where a program writes down what happens, one line for each thing. A web server’s has a line for each request it receives, and it’s the first thing you look at when something fails.Go to definition, a file where it writes down every visit of the day, one per line. Almost a thousand lines.
There are no windows on the server, so you can’t open it with a double click. And downloading it to search with the mouse won’t work the day the file is two gigabytes.
In lesson 1 you learned to move through folders. Now it’s time to work with what’s inside them: create, copy, delete, read and search. By the end you’ll know who went after the shop’s passwords.
The analogy
Section titled “The analogy”The analogy
Go back to lesson 1’s windowless building, with its literal receptionist. Now you ask them for office work: create a room (mkdir), photocopy a paper (cp), move it to another room or rename it (mv) and destroy it (rm). But this building has no trash can: it has a shredder.
For reading, they let you choose: they bring you the whole document at once (cat), let you leaf through it (less), read you the first or last pages (head and tail) or stand by the printer reading you each new page as it comes out (tail -f). And they go through a thousand pages in a second looking for a word (grep).
Where the analogy breaks down
- The receptionist doesn’t ask before overwriting something. If you photocopy a paper onto another one with the same name, the old one disappears without warning.
- Moving a file within the same disk doesn’t take it anywhere: it only changes the label that says which folder it’s in. That’s why it’s instant, even if the file is huge.
grepdoesn’t read like a person: it looks for exact letters, not ideas. If you search for “error”, it doesn’t find “failure”, or “ERROR” unless you ask it to.
How it really works
Section titled “How it really works”On a server, almost everything is text: the configuration, the programs’ messages, the scripts. That’s why these are the tools you’ll use the most.
Creating folders and files: mkdir and touch
Section titled “Creating folders and files: mkdir and touch”mkdir drafts creates the drafts folder where you are (from make directory). With -p, as in the introduction, it also creates any missing folders along the way: mkdir -p a/b/c creates all three at once.
touch notes.txt creates an empty file. If it already exists, it doesn’t change what’s inside: it only stamps it with the current time.
Copying, moving and renaming: cp and mv
Section titled “Copying, moving and renaming: cp and mv”cp (from copy) copies, and mv (from move) moves. Both take the source first and then the destination:
cp notes.txt copy.txtcreatescopy.txtwith the same contents asnotes.txt.cp notes.txt drafts/copies it into thedraftsfolder.cp -r drafts drafts-copycopies a whole folder. Without-r(from recursive, folder by folder),cpdoesn’t copy folders.mv notes.txt drafts/moves the file todrafts.mvmoves folders without needing-r.
In the terminal, renaming is moving: mv copy.txt old.txt moves the file to another name in the same folder. There’s no separate command.
And most important: cp and mv replace without warning. If the destination already exists, they overwrite it, and what was there is lost. If you want them to ask, add -i (from interactive): cp -i notes.txt copy.txt.
Deleting: rm has no trash
Section titled “Deleting: rm has no trash”rm notes.txt deletes the file (from remove). It doesn’t go to the trash, there’s no undo and it doesn’t ask.
rm -r draftsdeletes a folder with everything inside it. Without-r,rmdoesn’t delete folders.rmdir draftsdeletes a folder only if it’s empty: it’s the careful way.rm -i notes.txtasks before deleting. Answery(from yes) and Enter to delete; with anything else, it doesn’t delete.
You’ll see rm -rf a lot: the -f (from force) makes it ask nothing and not complain if something doesn’t exist. It’s the most dangerous instruction in the terminal, because one misplaced space changes what it deletes. Before running it, look with ls at what you’re about to delete.
Reading a file: cat, less, head and tail
Section titled “Reading a file: cat, less, head and tail”There are four ways to read, depending on what you want to see:
| Command | What it shows |
|---|---|
cat notes.txt |
The whole file, at once: for short files |
less access.log |
The file page by page: to go through a long one |
head access.log |
The first 10 lines |
tail access.log |
The last 10: the latest thing that happened |
cat doesn’t stop: if the file has a thousand lines, they all scroll past on the screen and you only see the last ones.
less takes up the whole window, like man in lesson 1 (in fact, man uses less). The keys are the same:
- Space moves forward a page, and B goes back.
- Shift + G jumps to the end, and G on its own, to the start.
- / searches: type some text and press Enter. N jumps to the next match.
- Q quits.
head and tail show 10 lines, and with -n you choose how many: head -n 3 access.log. And tail -f (from follow) shows the end and keeps waiting: every line added to the file appears on your screen. That’s how you watch a server live. You stop it with Ctrl + C.
Counting: wc
Section titled “Counting: wc”wc (from word count) counts the lines, words and bytes of a file:
$ wc access.log 974 21519 203864 access.logIt’s almost always used with -l, which counts only the lines. In a file with one line per visit, wc -l tells you how many visits there were.
Finding files by name: find
Section titled “Finding files by name: find”find goes through a folder and every folder inside it, and prints the path of whatever matches. find ~/backend-course -name "*.log" searches backend-course for files that end in .log:
- The first argument is where to start. If you start at
/, it goes through the whole disk: it will take a while and you’ll see lots ofPermission deniedfrom folders that aren’t yours. - The
*means “anything.” The quotes make it reachfindas is; without them, the shell could replace it with the names in the folder you’re in.
Searching inside files: grep
Section titled “Searching inside files: grep”find searches by name; grep searches inside. grep 404 access.log prints every line that contains 404, anywhere in the line. If none contains it, it prints nothing.
Its most useful options:
-iignores case:grep -i errorfindserror,ErrorandERROR.-c(from count) doesn’t print the lines: it says how many there are.-nputs each line’s number in the file in front of it.-vdoes the opposite: it prints the lines that don’t contain the text.-rsearches every file in a folder and its subfolders.
What grep searches for is a pattern, not just text: some characters mean something else. The dot (.) is “any one character,” and .*, “anything.” So grep 'ana.*404' looks for lines that have ana and, somewhere after it, 404. If the pattern has spaces or symbols, put it in single quotes.
Editing on the server: nano
Section titled “Editing on the server: nano”There’s no VS Code on a server. You use an editor that runs inside the terminal, and the easiest is nano. nano notes.txt opens the file (or creates it) and takes up the window:
- You type and move around with the arrow keys.
- At the bottom there’s a cheat sheet with the shortcuts. The
^means Ctrl:^X Exitis Ctrl + X to exit. - Ctrl + O saves: the file name appears at the bottom, and Enter confirms it.
- Ctrl + X exits. If you haven’t saved, it asks you: Y saves (and Enter confirms the name), N exits without saving.
On the Mac, nano is actually pico, the editor nano was born from: the top says UW PICO, but the keys are the same. On Ubuntu and WSL it’s GNU nano.
Getting out of vim
Section titled “Getting out of vim”vim (or vi) is the other editor that’s on almost every server, famous because many people don’t know how to get out of it. Sometimes it shows up without you asking: on the Mac, Git opens it to ask you for a commit message if you haven’t told it to use another editor.
The odd thing is that it has modes: when you open it, the keys don’t type, they’re commands. With I you switch to typing, and with Esc you go back to commands. To get out:
- Press Esc, in case you were typing.
- Type
:q!and press Enter: you quit without saving. What you type appears at the very bottom. - If you want to save, type
:wqinstead of:q!(write and quit).
Try it
Section titled “Try it”The goal: find out who tried to steal the shop’s secrets, at what time and whether they succeeded. First, set up a folder for your notes.
Everything happens in your practice folder (if you don’t have it yet, create it as in the introduction), and you only delete what you create here. The outputs are from a Mac, with the user replaced by ana; in each exercise we tell you what changes on Linux and WSL.
1. Create, copy and rename
cd ~/backend-course/phase-1mkdir draftstouch drafts/notes.txtcp drafts/notes.txt drafts/copy.txtmv drafts/copy.txt drafts/old.txtls draftsWhat you will see:
notes.txt old.txtYou create the folder, an empty file inside it, copy it and rename the copy: copy.txt no longer exists, because mv changed its name. Only ls prints anything.
If mkdir answers File exists, you already had a drafts folder: carry on with the other lines. On Linux and WSL, the columns of ls are separated by two spaces.
Now write in your notes with nano. This is done with the keyboard:
- Type
nano drafts/notes.txtand press Enter. - Type
Clues from the log:and press Enter. - Save with Ctrl + O and Enter, and exit with Ctrl + X. On the Mac it’s also Ctrl, not Cmd.
2. Check what you wrote
cat drafts/notes.txtWhat you will see:
Clues from the log:cat shows you the whole file: your line. If it comes out empty, you didn’t actually save: open it again and don’t forget the Enter after Ctrl + O.
3. Delete with a safety net
rm -i drafts/old.txtls draftsWhat you will see:
remove drafts/old.txt? ynotes.txtrm -i asks before deleting: type y and press Enter. ls confirms that only notes.txt is left. And don’t look for old.txt in the trash: it isn’t there.
On Linux and WSL the question is longer: rm: remove regular empty file 'drafts/old.txt'?.
Now, the case. We’ve left the shop’s log from October 1 on this site, with one line for every request its server received. The shop is made up, but the format is the one real web servers use.
4. Download the log
curl -O https://backenddesdecero.com/practicas/access.logls -l access.logWhat you will see:
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed100 199k 100 199k 0 0 2995k 0 --:--:-- --:--:-- --:--:-- 3016k-rw-r--r--@ 1 ana staff 203864 Oct 12 15:29 access.logWith -O (a capital O), curl saves what it downloads in a file with the name it has in the address. The first three lines are its progress bar: the 100 at the start means 100% has arrived.
ls -l confirms the file is there and is 203,864 bytes. Your speed and your date will be different. On Linux and WSL, the owner shows as ana ana and the permissions may be -rw-rw-r--.
5. How many requests were there?
wc -l access.logWhat you will see:
974 access.log974 requests in one day: too many to read one by one. On Linux and WSL, the number comes out with no spaces in front.
6. What a line looks like
head -n 1 access.logWhat you will see:
203.0.113.50 - - [01/Oct/2026:00:00:41 +0000] "GET /robots.txt HTTP/1.1" 200 12378 "-" "ExampleBot/1.0 (+https://example.com/bot)"Before searching, understand what each piece of the day’s first request says:
| Piece | What it is |
|---|---|
203.0.113.50 |
The IP addressIP stands for Internet Protocol.The number that identifies a computer on a network, for example 192.168.1.133 (IPv4) or 2606:4700:10::6814:179a (IPv6). It is the address the network layer uses to take each packet to its destination.Go to definition of whoever made the request |
[01/Oct/2026:00:00:41 +0000] |
The date and time. +0000 is Coordinated Universal Time (UTC): in Spain, in October, add two hours |
"GET /robots.txt HTTP/1.1" |
The request, like the ones you wrote in the lesson on protocols |
200 |
The Status codeA three-digit number at the start of every HTTP response that sums up what happened. 200 means “all good,” 404 “that doesn’t exist” and 400 “your request is malformed.”Go to definition: 200 means everything went fine, and 404, that the thing doesn’t exist |
12378 |
The bytes the server sent |
"ExampleBot/1.0 …" |
The program that made the request: a browser, a bot… |
The dashes are data this server doesn’t record. And the IPs in the log are reserved for examples: they belong to nobody.
Take a stroll through the file, with no output to copy:
- Type
less access.log. Move forward with Space, jump to the end with Shift + G and go back to the start with G. - Type
/envand press Enter:lessjumps to the first line withenvand highlights it. Quit with Q. - Type
tail -f access.log. The last 10 lines appear and the terminal keeps waiting. Nothing will arrive here, because nobody writes to your copy; on a real server, you’d see each visit appear. Stop with Ctrl + C.
A website’s passwords and keys are often kept in a .env file (you’ll see it in lesson 7). Whoever wanted to steal them will have requested it:
7. Find who requested the secrets
grep env access.logWhat you will see:
198.51.100.23 - - [01/Oct/2026:03:12:23 +0000] "GET /.env HTTP/1.1" 404 153 "-" "python-requests/2.32.3"198.51.100.23 - - [01/Oct/2026:03:12:24 +0000] "GET /.env.backup HTTP/1.1" 404 153 "-" "python-requests/2.32.3"198.51.100.23 - - [01/Oct/2026:03:12:50 +0000] "GET /.env HTTP/1.1" 404 153 "-" "python-requests/2.32.3"198.51.100.23 - - [01/Oct/2026:03:12:51 +0000] "GET /.env.backup HTTP/1.1" 404 153 "-" "python-requests/2.32.3"Four lines out of 974. The IP 198.51.100.23 requested /.env and /.env.backup at 3:12 in the morning (5:12 a.m. in Spain), twice each. And not with a browser: python-requests is a program that makes requests by itself. It’s a scanner, a program that goes through websites trying files that often hold secrets.
If nothing comes out, check with ls that you’re in the folder where you downloaded the log.
8. How many times did it try?
grep -c 198.51.100.23 access.logWhat you will see:
40The same IP appears on 40 lines: it tried twenty different files, twice each, in less than a minute. Without -c, grep shows you all of them.
9. Where is the log?
find ~/backend-course -name "*.log"What you will see:
/Users/ana/backend-course/phase-1/access.logfind goes through all of backend-course and only finds one .log: the one you downloaded. On Linux and WSL, the path starts with /home/ana.
And now, the challenge. Type the instructions yourself, without copying them:
Challenge: did the scanner get away with anything? Check whether any of its 40 requests didn’t end in 404. Then find which lines of the log asked for wp-login.php, the login page of websites built with WordPress, and write it down in your notes with nano.Show answer
grep -c '198.51.100.23.* 404 ' access.loggrep -n wp-login access.lognano drafts/notes.txtThe first instruction answers 40: all 40 requests from that IP ended in 404, so it got nothing. The pattern looks for the IP and, somewhere after it, 404 with its spaces, so it isn’t confused with a 404 inside another number.
The second answers with two lines, 46 and 66, both with 404. The shop doesn’t use WordPress: the scanner tries it on every website.
To clean up, rm -r drafts. But don’t delete access.log: you’ll use it in lesson 3, “Pipes and redirection”.
You have already seen it
Section titled “You have already seen it”You already did all of this with the mouse:
- The trash is what
rmdoesn’t have. When you drag a file to the trash, you move it to a hidden folder: it’s anmv, not anrm. - Renaming a file or dragging it to another folder in the Finder or in Windows File Explorer is an
mv. But they warn you if there’s already one with that name;mvdoesn’t. - Cmd + F on a web page is a
grepof a single page, and the Finder’s search by name, afind.
And if you code:
- VS Code’s global search (Cmd + Shift + F) is a
grep -r. - What
npm run devprints while you work is the same thing you see withtail -f: a program telling you what it’s doing.
Common mistakes
Section titled “Common mistakes”- A name with spaces in
rm, without quotes.rm my notes.txttries to delete two files,myandnotes.txt. If they don’t exist, it complains about both (rm: my: No such file or directory); if they do, it deletes them. Use quotes,rm "my notes.txt", or Tab. - Forgetting
-rwith folders.rm draftsanswersrm: drafts: is a directory, andcp drafts copy,cp: drafts is a directory (not copied).On Linux and WSL:rm: cannot remove 'drafts': Is a directoryandcp: -r not specified; omitting directory 'drafts'. - Searching for an IP that is the start of another one. In the log,
grep -c 203.0.113.5 access.loganswers167, but that IP only made 4 requests:grepalso counts203.0.113.50,203.0.113.53and203.0.113.55, which start the same way. Add the space that comes after the IP:grep -c '203.0.113.5 ' access.loganswers4.
Summary
Section titled “Summary”mkdircreates folders andtouch, empty files.cpcopies andmvmoves or renames, and both replace without warning.rmdeletes forever: there’s no trash. For folders,-r; to make it ask,-i. Look withlsbefore deleting.- For reading:
catif it’s short,lessif it’s long,headandtailfor the start and the end, andtail -fto follow a file that grows.wc -lcounts the lines. findfinds files by name, andgrep, text inside them.- To edit on a server,
nano: Ctrl + O saves and Ctrl + X exits. If you land invim: Esc,:q!and Enter.
Did you get it?
Section titled “Did you get it?”To review:
Further reading
Section titled “Further reading”- The Linux Command Line, by William Shotts: the chapters “Manipulating Files and Directories” and “Regular Expressions” go beyond this lesson.
- The GNU grep manual: every
grepoption and how patterns are written.