Linux Filesystem Commands Every Beginner Should Know
Table of Contents
All the data on your computer is organized into files and directories, and as a developer you'll spend a lot of time creating, moving, reading, and deleting them from the command line. This guide covers the core Linux filesystem commands: pwd, ls, and cd for navigation, cat, head, tail, and less for reading files, touch, mkdir, mv, rm, and cp for managing them, and grep and find for searching.
All the content from our Boot.dev courses are available for free here on the blog. This one is the "Filesystems" chapter of Learn Linux. If you want to try the far more immersive version of the course, do check it out!
What Is a Filesystem?
All the data stored on your computer is organized into files and directories. Files and directories are organized into a tree-like structure called a filesystem.

- Directories (or "folders" on Windows) are just containers that hold files and other directories.
- Files are just a dump of raw binary data: 1's and 0's. The bytes in a file can represent anything: text, images, videos, etc.
The filesystem tree starts with a single directory called the root directory. The root directory contains files and directories, which can contain more files and directories, and so on.
When you open your terminal, your working directory (the one you're "in") is going to be... somewhere. Most commonly it's your home directory (more on that later). The pwd command prints your current working directory:
pwd
# /home/wagslane
How Do Filepaths Work?
Click to play video
The output of pwd is a filepath. A filepath is a string that describes the location of a file or directory on your computer. On Linux (or Windows with WSL), mine looks like this:
/home/wagslane
- The first slash (
/) represents the root directory. It's the tippy-top of the filesystem tree. - The next part (
home) is the name of a directory inside the root directory. - Finally, the last part (
wagslane) is the name of a directory inside thehomedirectory.
So this path represents a directory 2 levels down from the root directory:
root
└── home
└── wagslane
macOS works the same way, but home directories live in /Users instead of /home, so on my Mac the same path is /Users/wagslane.
How Do You Navigate the Filesystem?
The ls ("list") command prints the contents of a directory. You can pass it a directory path as an argument to list that directory without moving into it:
ls
# projects documents
ls documents
# passwords.txt resume.pdf
The cd command "changes directory" to move into a directory:
cd dungeon
pwd
# /home/wagslane/projects
But how do you move back out of the current directory? The answer is two dots: ..
.. is a special alias that refers to "the parent directory." It's a shortcut that you can use to move up one level in the directory tree. If you chain multiple .. segments together, you'll move up that many directories:
cd ../..
pwd
# /home
While you're navigating, the tab key is your friend! If you start typing the name of a file or directory and then press Tab, your shell will try to autocomplete the name for you. If there are multiple possible completions, it will show you a list of them. I rarely type out full file names, I type the first few characters and then press Tab.
Absolute vs. Relative Paths
There are two kinds of filepaths: relative and absolute. A relative path takes your current directory into account. For example, let's say we have the following directory structure in our filesystem:
vehicles
├── cars
│ ├── fords
│ │ ├── mustang.txt
│ │ └── focus.txt
When inside the top-level vehicles directory, the relative path to the mustang.txt file is:
cars/fords/mustang.txt
However, when we're inside the cars directory, the relative path to the mustang.txt file is just:
fords/mustang.txt
Or when inside the fords directory, just:
mustang.txt
An absolute path is a path that starts at the root of the filesystem. On Unix-like systems (macOS/Linux), the root is denoted by a forward slash /. So, if the vehicles directory is in the filesystem root, the absolute path to the mustang.txt file is:
/vehicles/cars/fords/mustang.txt
That absolute path points at the same file no matter which directory you're in.
Which Should You Use?
It depends.
Relative paths are easier to read and write, and as long as you're in the correct directory (or the directory you expect), they're easier to reason about.
Absolute paths are more explicit. They're useful when you're not sure what directory you're currently in. For example, maybe you're giving someone instructions on how to find a file on their computer. You can't be sure what directory they'll be in when they start following your instructions, so you'll need to use an absolute path.
How to View a File's Contents with cat
At their core, files are just blobs of data. The raw bytes in a file can represent anything: text, images, videos, etc.
The cat command is used to view the contents of a file. It's short for "concatenate," which is a fancy way of saying "put things together." It can feel like a confusing name if you're using the cat command to view a single file, but it makes more sense when you're using it to view multiple files at once:
cat quotes.txt
# It's dangerous to go alone!
# Do a barrel roll!
cat quotes.txt more_quotes.txt
# It's dangerous to go alone!
# Do a barrel roll!
# The cake is a lie
The head and tail Commands
Sometimes you don't want to print everything in a file. Files can be really big after all.
The head command prints the first n lines of a file. The -n flag specifies n as shown:
head -n 2 transactions.csv
# id,amount,date
# 1,50.00,2023-01-01
If you don't specify a number using the -n flag, it will default to 10.
The tail command prints the last n lines of a file:
tail -n 1 transactions.csv
# 5089,20.00,2023-12-31
These two are perfect for peeking at huge files, like a CSV with thousands of rows where you only care about the header and the most recent entries.
When Should You Use less Instead of more?
The more and less commands let you view the contents of a file, one page (or line) at a time.
As the adage goes, "less is more".
In the context of these commands, less is literally more. The less command does everything that the more command does but also has more features. As a general rule, you should use less instead of more. You would only use more if you're on a system that doesn't have less installed (and if that's the case, you can probably grab it with a package manager).
less and more are interactive pagers: they take over your terminal window so you can scroll through a file a page at a time, and you press Q to quit back to your shell prompt:
less transactions.csv
How to Create Files with touch
The touch command updates the access and modification timestamps of a file. By default, if the specified file does not exist, touch will create an empty file with the given filename. Because of this side-effect, you'll often see this command used to quickly create new empty files:
touch credithistory.txt
ls
# credithistory.txt
You can also create multiple files at once by listing them:
touch some_file.txt some_other_file.txt
touch can be very handy when writing scripts because it ensures files exist without altering existing ones, creating new files only if necessary.
How to Create a Directory with mkdir
A directory is just a location in a filesystem that can contain files and other directories. On some systems, directories are called "folders," but it's the same thing.
The "make directory" command creates a new directory inside (or relative to) the current directory:
mkdir cds
ls
# cds credit_cards loans
How to Move and Rename Files with mv
The move command moves a file or directory from one location to another. You can use it to rename a file or to move it to a different directory altogether. Your working directory can't be the directory you're moving.
Renaming a file:
mv some_file.txt some_other_name.txt
Moving a file from the current directory to another nested directory:
mv some_file.txt some_directory/some_file.txt
Moving a file from the current directory, to the parent directory:
mv some_file.txt ../some_file.txt
If you don't want to rename the file and you're just moving it to a different directory, you can omit the filename:
mv some_file.txt some_directory/
Both the source and destination have to be valid paths from the current working directory. Use pwd to see where you are and adjust the paths accordingly, then ls to verify the file landed where you expected. If you mess up an mv, you'll need to figure out where you accidentally moved the file to, then move it from that location back to where it belongs.
How to Delete Files and Directories with rm
The remove command deletes a file or empty directory:
rm some_file.txt
You can optionally add a -r flag to tell the rm command to delete a directory and all of its contents recursively. "Recursively" is just a fancy way of saying "do it again on all of the subdirectories and their contents."
rm -r some_directory
There's no recycle bin in the terminal. When rm deletes something, it's gone.
How to Copy Files with cp
The copy command does what you would (hopefully) expect: it copies a file from one location to another:
cp source_file.txt destination/
You can also copy a directory and all of its contents recursively by adding the -R flag:
cp -R my_dir new_dir
On most Linux systems, the -r and -R flags for cp both mean "recursive." We used lowercase -r with rm and uppercase -R here, but either will work for recursive copy on most distros.
What Is the Home Directory?
In a Unix-like operating system, a user's home directory is the directory where their personal files are stored. It is also the directory that a user starts in when logging into the system.
I recommend doing all of your development work in your home directory. For example, I like to create a workspace directory in my home directory, and all my programming projects live in subdirectories there.
Your home directory is where you want to spend most of your time. Many of the other directories on your machine are critical to the operating system or other programs. Be careful when working in other directories like /bin (where executable programs live), /etc, /var, etc. You can mess up your system if you're not careful, especially once you start changing file permissions.
The ~ character is an alias for your home directory. So when I want to go home, I don't have to type out cd /Users/wagslane, I can just type:
cd ~
How to Search for Text in Files with grep
You might be used to nice graphical interfaces that allow you to search for text in files, usually with Ctrl+F or Cmd+F. But what about when you're working in a terminal?
As it turns out, once you're used to it, searching for text in files in a CLI can be much faster than using a GUI.
The grep command allows you to search for text in files. It has a ton of capability, and we'll only be scratching the surface of its true power.
The most basic use for grep is to search for a string in a file. For example, if we wanted to search for the word "CRITICAL" in the file 2024-01-10.log, we could run:
grep "CRITICAL" 2024-01-10.log
# 2024-01-10 06:33:11 CRITICAL disk usage at 92 percent
This will print out every line in the file that contains the text CRITICAL. It's a case-sensitive search, so it will only match CRITICAL, not critical or Critical.
You can also search multiple files at once:
grep "CRITICAL" 2024-01-10.log 2024-01-11.log
And you can search an entire directory, including all subdirectories, with the -r (recursive) flag:
grep -r "CRITICAL" logs
To search the current directory, use ., which is a special alias for the current directory:
grep -r "CRITICAL" .
grep gets even more powerful once you start feeding it input through pipes, but plain file searching will get you a long way.
How to Find Files by Name
The find command is a powerful tool for finding files and directories by name, not by their contents. (That's the difference between find and grep: grep searches inside files, find searches for the files themselves.)
Let's say you're looking for a file named hello.txt somewhere in your home directory. You can use the find command to search for exactly that title:
find ~ -name hello.txt
# /home/wagslane/notes/hello.txt
The find command can also search for files that match a pattern. For example, if you wanted to find all files that end in .txt, you could run:
find some_directory -name "*.txt"
The * character is a wildcard that matches anything (the same idea shows up in .gitignore patterns). If you're trying to find filenames that contain a specific word, you can use the * character to match the rest of the filename:
find some_directory -name "*joint*"
# some_directory/joint_savings.txt
# some_directory/joint_checking.txt
Once you're comfortable moving around the filesystem, creating and destroying files, and searching with grep and find, you've got the foundation that everything else in Learn Linux (and frankly, the whole DevOps path) builds on. Good next steps are learning how shells and terminals actually differ, and how your shell configuration works under the hood.
Frequently Asked Questions
What is the difference between an absolute and a relative path?
An absolute path starts at the root of the filesystem and always begins with a forward slash, like /home/wagslane/notes.txt. A relative path is interpreted from your current working directory, like notes.txt. Absolute paths point to the same location no matter where you are; relative paths depend on where you currently are.
What does the ~ symbol mean in Linux?
The tilde (~) is an alias for your home directory. Running cd ~ takes you home from anywhere, and paths like ~/workspace expand to a path inside your home directory.
How do I go up one directory in the terminal?
Run cd .. — the two dots are a special alias for the parent directory. You can chain them, like cd ../.., to move up multiple levels at once.
What is the difference between grep and find?
grep searches for text inside files, while find searches for files and directories by their names. Use grep when you know what a file contains, and find when you know what a file is called.
Does rm move files to the trash?
No. rm deletes files immediately and permanently — there is no recycle bin or trash folder in the terminal. Double-check the path before running rm, especially with the -r flag, which recursively deletes a directory and everything inside it.
