by Seth Kenlon
Reprinted from Opensource.com
Creative Commons Attribution-Share Alike 4.0 International License
Here are five common file management tasks you can do with nothing but the shell.
Image by: iradaturrahmat via Pixabay, CC0
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 ls (that's a lower case L and a lower case S).
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 to 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:
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:
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:
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:
A few points:
- The find command 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 $HOME to tell find to look through my personal data as opposed to system files.
- The -iname option tells find to search for a file by name, ignoring capitalization.
- Finally, the "*holiday*" argument tells find that the word "holiday" appears somewhere in the filename. The * characters are wildcards, so find locates 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 command:
You can't cd to a file, though:
4. Open a file
If you've got a file you want to open from a terminal, use the xdg-open command:
Alternatively, you can open a file in a specific application:
5. Copy or move a file or folder
The 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:
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 old-example.txt:
You don't actually have to move a file from one directory to another just to rename it:
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 how to use a terminal, there's no time like the present, so get started today!