A terminal is an application that provides access to the user shell of an operating system (OS). Traditionally, the shell is the place where the user and the OS could interface directly with one another. And historically, a terminal was a physical access point, consisting of a keyboard and a readout (a printer, long ago, and later a cathode ray tube), that provided convenient access to a mainframe. Don't be fooled by this "ancient" history. The terminal is as relevant today as it was half a century ago, and in this article, I provide five common file management tasks you can do with nothing but the shell.
1. Open a terminal and look around
Today, everyone's got a computer on their desk or in their bag. The mainframe-and-terminal model is now essentially emulated through an application. Your operating system might have a unique name for it, but generically it's usually known as a "terminal" or "console".
Linux: Look for Console, Konsole, or Terminal. Regardless of the name, you can usually launch it from your application menu using the key word "terminal."
macOS: The default terminal application isn't open source and is widely considered lacking in features. Download iTerm2 to get a feature-rich, GPLv2 replacement.
Windows: PowerShell is the open source terminal application, but it uses a language and syntax all its own. For this article to be useful on Windows, you can install Cygwin which provides a POSIX environment.
Once you have your terminal application open, you can get a view of your file system using the command
2. Open a folder
In a graphical file manager, you open a folder by double-clicking on it. Once it's open, that folder usually dominates the window. It becomes your current location.
In a terminal, the thought process is slightly different. Instead of opening a folder, you change to a location. The end result is the same: once you change to a folder, you are "in" that folder. It becomes your current location.
For example, say you want open your Downloads folder. The command to use is
cd plus the location you want to change to:
To "close" a folder, you change out of that location. Taking a step out of a folder you've entered is represented by the
cd command and two dots (
You can practice entering a folder and then leaving again with the frequent use of
ls to look around and confirm that you've changed locations:
$ cd Downloads $ ls cat-photo.jpg $ cd .. $ ls Documents Downloads Music Pictures Videos $ cd Documents $ ls zombie-apocalypse-plan-C.txt zombie-apocalypse-plan-D.txt $ cd .. $ ls Desktop Documents Downloads Music Pictures Videos
Repeat it often until you get used to it!
The advanced level of this exercise is to navigate around your files using a mixture of dots and folder names.
Suppose you want to look in your Documents folder, and then at your Desktop. Here's the beginner-level method:
$ cd Documents $ ls zombie-apocalypse-plan-C.txt zombie-apocalypse-plan-D.txt $ cd .. $ ls Desktop Documents Downloads Music Pictures Videos $ cd Desktop $ ls zombie-apocalypse-plan-A.txt
There's nothing wrong with that method. It works, and if it's clear to you then use it! However, here's the intermediate method:
$ cd Documents $ ls zombie-apocalypse-plan-C.txt zombie-apocalypse-plan-D.txt $ cd ../Desktop $ ls zombie-apocalypse-plan-A.txt
You effectively teleported straight from your Documents folder to your Desktop folder.
There's an advanced method, of this, too, but because you know everything you need to know to deduce it, I leave it as an exercise for you. (Hint: It doesn't use
cd at all.)
3. Find a file
Admit it, you sometimes misplace a file. There's a great Linux command to help you find it again, and that command is appropriately named
$ find $HOME -iname "*holiday*" /home/tux/Pictures/holiday-photos /home/tux/Pictures/holiday-photos/winter-holiday.jpeg
A few points:
findcommand requires you to tell it where to look.
Casting a wide net is usually best (if you knew where to look, you probably wouldn't have to use
find), so I use
findto look through my personal data as opposed to system files.
findto search for a file by name, ignoring capitalization.
findthat the word "holiday" appears somewhere in the filename. The * characters are wildcards, so
findlocates any filename containing "holiday", whether "holiday" appears at the beginning, middle, or end of the filename.
The output of the
find command is the location of the file or folder you're looking for. You can change to a folder using the
$ cd /home/tux/Pictures/holiday-photos $ ls winter-holiday.jpeg
cd to a file, though:
$ cd /home/tux/Pictures/holiday-photos/winter-holiday.jpeg cd: Not a directory
4. Open a file
If you've got a file you want to open from a terminal, use the
$ xdg-open /home/tux/Pictures/holiday-photos/winter-holiday.jpeg
Alternatively, you can open a file in a specific application:
$ kate /home/tux/Desktop/zombie-apocalypse-plan-A.txt
5. Copy or move a file or folder
cp command copies and the
mv file moves. You can copy or move a file by providing the current location of the file, followed by its intended destination.
For instance, here's how to move a file from your Documents folder to its parent directory:
$ cd Documents $ ls zombie-apocalypse-plan-C.txt zombie-apocalypse-plan-D.txt $ mv zombie-apocalypse-plan-C.txt .. $ cd .. $ ls Documents Downloads Music Pictures Videos zombie-apocalypse-plan-C.txt
While moving or copying, you can also rename it. Here's how to move a file called
example.txt out of the directory with the new name
$ mv example.txt ../old-example.txt
You don't actually have to move a file from one directory to another just to rename it:
$ mv example.txt old-example.txt
Linux terminal for files
The Linux desktop has a lot of file managers available to it. There are simple ones, network-transparent ones, and dual-panel ones. There are ones written for GTK, Qt, ncurses, and Swing. Big ones, small ones, and so on. But you can't talk about Linux file managers without talking about the one that's been there from the beginning: the terminal.
The terminal is a powerful tool, and it takes practice to get good at it. When I was learning the terminal, I used it for what I could, and then I opened a graphical file manager for advanced operations that I hadn't learned for the terminal yet. If you're interested in learning the how to use a terminal, there's no time like the present, so get started today!