Want to start leveling up your terminal skills? The Linux command line has a wealth of flexible commands for file management. Learn how to use the most common ones for your everyday tasks.
1 pwd
When you’re using the command line, you’re always “in” a specific directory. Linux calls this the working directory. By default, your prompt—the text before your cursor on each line—will display the name of this directory. It’s usually your home directory when you first open a terminal.
The pwd command stands for “print working directory” and you can think of it like a “you are here” marker. When you run pwd, it tells you the full (absolute) path of your working directory:
2 cd
Now you know where you are, it’s time to learn how to move around your filesystem with the cd command, which stands for “change directory.” Your working directory is important because all commands and files that use relative paths do so from that directory.
Run the cd command by supplying a directory as the first argument. This directory can be a complete absolute path:
cd /usr/local/bin
Or it can be relative to your current directory:
cd bin
cd ../docs
You can call cd without any arguments to quickly return to your home directory:
cd
3 ls
Once you’ve navigated to a directory, you’ll often want to know what files (including subdirectories) it contains. The ls command stands for “list” and, unlike the previous two commands, it has many options.
Use ls on its own to show the contents of the current directory:
ls
You can list specific files or directories by passing them as arguments. Use the -l flag (long format) to display one file per line with full information including permissions, ownership, size, and modification date/time:
ls -l /boot
This format also displays the type of each file. If the first letter of the permission string is a “d”, it’s a directory. If it’s an “l”, it’s a link. If it’s a “-“, it’s a regular file.
If you want a more obvious indicator, try the -F option. It adds a “https://www.howtogeek.com/” to the end of directories and an “@” to the end of links, making them easier to spot:
ls -F /bin/*.grep
Note that you can include wildcard characters to list files matching a pattern. You can use wildcards with any command that accepts file arguments, not just ls.
4 touch
This strange-sounding command lets you update a file’s access & modification times. You might not think you’d need this much, and you’re probably right, but touch has a secondary purpose: to create an empty file.
Run the command with any number of arguments, then touch will update existing files and create new ones:
touch foo bar
5 mkdir
To organize your filesystem, you’ll want to create directories to group related files. Remember that directories can contain subdirectories, and so on. The mkdir command is simple: it creates a directory at the path of each argument you supply.
mkdir foo foo/bar hum
Note that each path can be relative or absolute. If you try to create multiple levels of directory at the same time, mkdir will complain:
Use the -p flag to create the full hierarchy in one go:
mkdir -p one/two/three
6 cp
To create an additional copy of an existing file, use the cp (copy) command. In its simplest form, give the path of an existing file and a path for the new copy:
cp resume.pdf resume2.pdf
You can also use this command to copy several files to a directory. To do so, use as many file arguments as you like, followed by a single directory:
cp file1.md file2.md file3.md myfiles
7 rm / rmdir
Eventually, you’ll find you’ve created too many files and you’ll want to get rid of some. Enter rm, short for “remove.”
The rm command takes one or more files as arguments and will attempt to delete each one:
rm resume *.bak
Use the -f option to “force” removal of a file. This attempts to delete a file, even if its permissions appear to restrict you from doing so:
rm -f filename
For example, without write permission, you’ll be prompted for confirmation if you try to delete a file. The -f flag skips this step; it also suppresses an error message if the file doesn’t exist.
rm will also let you remove directories, but not by default:
The -d option will let you do so, as will rmdir:
8 mv
Initially, it might seem like the mv command (short for “move”) has a dual-purpose: both renaming files and moving them from one directory to another. However, Linux considers both just ways of changing a file’s path.
mv old-name new-name
With two arguments, mv will rename—or move—the first to the second:
mv has another mode that works a bit like cp’s second mode. If the final argument is a directory, mv will move all other named files into that directory, even those that are directories.
9 chmod
On Linux, each file has a set of permissions describing who can access it and what they can do with it. The “who” can be the owner of the file, anyone in the same group as the file, or anyone at all. That “what” can be reading, writing, or executing—running the file as a program.
Use of chmod is a bit complicated because of its permissions syntax. Basic use is like this:
chmod files
An example of permissions is “go+r” which makes (+) a file readable (r) to users in its group (g) and other users (o). Another typical use is to make a file executable:
chmod a+x script.sh
This lets you run a script on the command line by typing its path.
10 ln
Links let you reference files in more than one place on the file system, without taking up much extra space. The symbolic—or soft—link is the easiest to understand:
ln -s original link
The link command creates a link that references an original file:
Without the -s option, you’ll create a hard link, which acts more like a second copy of the original file. They are a bit harder to understand and, therefore, are less commonly used.
There are many more important Linux commands you should learn to do anything from killing a process to fetching a remote web page.