Chapter 114·Beginner·9 min read
Working with Files from the Command Line
A plain-English guide to handling files in the Linux terminal — creating folders and files, viewing contents with cat and less, copying, moving, renaming and deleting, wildcards, and finding files with find and their contents with grep. The everyday verbs of getting work done in a shell.
July 17, 2026
You can now find your way around the filesystem. This chapter gives you the verbs — the everyday actions of making, viewing, moving, deleting, and finding files. Each is a small command, and together they cover essentially everything a graphical file manager does, with far more precision. We'll end with the two search tools that let you locate any file, or any line inside any file, on the whole system.
Making things: mkdir and touch
To create a directory, use mkdir (make directory):
$ mkdir reports
$ mkdir -p 2026/q3/data # -p makes the whole nested path at onceThat -p flag is quietly useful: without it, mkdir refuses to create 2026/q3/data unless 2026/q3 already exists. With it, the shell creates every missing level in one go.
To create an empty file, use touch:
$ touch notes.txttouch has a second job — if the file already exists, it updates the file's "last modified" timestamp to now without changing the contents. But day to day, you'll reach for it to conjure an empty file into being.
Looking inside: cat and less
Two commands show you a file's contents, and choosing the right one matters more than it seems.
cat (short for concatenate) prints the entire file to the screen at once. It's perfect for something short:
$ cat notes.txt
Buy milk
Ship the releaseBut run cat on a 50,000-line log file and it will flood your screen, scrolling far past anything you can read. For anything long, use less instead. It opens the file in a scrollable, searchable viewer that loads instantly even for huge files:
$ less /var/log/system.logInside less: arrow keys or the space bar scroll, /word searches forward for "word", n jumps to the next match, G goes to the end, g to the top, and q quits back to the shell. (That last one saves a lot of "how do I get out of this?" panic.)
Copying and moving: cp and mv
cp (copy) duplicates a file. The shape is always source, then destination:
$ cp app.py app.backup.py # copy to a new name, same folder
$ cp app.py ~/backups/ # copy into another folder, same name
$ cp -r project project-copy # -r copies a whole folder and its contentsThe -r ("recursive") flag is required to copy a directory, because by default cp only handles single files; -r tells it to descend into the folder and copy everything inside.
mv (move) relocates a file instead of duplicating it — the original doesn't stay behind. Same source, destination shape:
$ mv report.txt ~/documents/ # move it into another folderHere's the Unix twist that surprises newcomers: there is no rename command. To rename a file, you move it to a new name in the same directory:
$ mv draft.txt final.txt # 'renames' draft.txt to final.txtIt makes sense once you see it their way: a file's name and its location are the same kind of thing — its path — so changing either one is just... moving it.
Deleting: rm, and a serious warning
rm (remove) deletes files. It is fast, it is final, and it deserves your full attention:
$ rm notes.txt # delete a file
$ rm -r old-project # delete a folder and everything in itThe classic disaster is combining rm -r with a wildcard or a stray space and deleting far more than intended. A good habit: run the same pattern with ls first to see exactly what it matches, and only then swap ls for rm.
Wildcards: acting on many files at once
This is where the terminal starts to feel powerful. The shell lets you use wildcards (also called globbing) to match many files with a pattern:
*matches any run of characters.*.txtmatches every file ending in.txt.?matches exactly one character.report-?.txtmatchesreport-1.txtbut notreport-42.txt.[...]matches any one character from a set.img-[123].pngmatchesimg-1.png,img-2.png,img-3.png.
$ ls *.log # every .log file here
$ cp *.txt ~/backups/ # copy all text files at once
$ rm temp-* # delete everything starting with 'temp-'The shell expands the pattern into the full list of matching files before the command runs — so rm temp-* becomes rm temp-1 temp-2 temp-notes behind the scenes. That's exactly why the "run it with ls first" habit works: ls temp-* shows you the very same list rm is about to act on.
Finding files: find
Once a system has thousands of files, "I know it's here somewhere" needs a tool. find walks the directory tree looking for files that match a condition. You give it a place to start and something to match:
$ find . -name "*.log" # every .log file under the current folder
$ find ~ -name "config.json" # a specific file anywhere in your home
$ find . -type d -name "test*" # only directories whose name starts 'test'
$ find . -name "*.tmp" -mtime -1 # .tmp files changed in the last dayfind reads a little differently from other commands: . is where to look (here, and everything below), and the -name/-type/-mtime parts are conditions it must satisfy. It's exhaustive — it will search every corner of the tree you point it at.
Finding inside files: grep
find locates files by their name. Often, though, you don't know the filename — you know a word that's inside it. That's grep: it searches the contents of files for lines matching a pattern and prints those lines.
$ grep "error" server.log # lines containing 'error'
$ grep -i "error" server.log # -i: ignore case (Error, ERROR too)
$ grep -r "API_KEY" . # -r: search every file under here
$ grep -n "TODO" app.py # -n: show line numbersgrep is one of the most-used commands in all of Unix, because "show me every line that mentions X, across all these files" is a question you ask constantly — hunting a bug in logs, finding where a function is called, tracking down a stray configuration value.
The toolkit so far
Step back and notice what you now hold: mkdir and touch to create, cat and less to view, cp and mv to duplicate and relocate, rm to delete, wildcards to act in bulk, and find/grep to locate anything. That's the full everyday vocabulary of file work — and every one of these commands follows the command, options, arguments shape from chapter one.
But so far each command has stood alone. The real magic of the Unix command line is that these small tools were built to connect — to pass their output straight into one another and form pipelines that solve problems no single command could. That's the next chapter, and it's where the terminal stops being a faster file manager and becomes something genuinely different.