Managing files with the Linux terminal
![Stack of files](/sysadmin/sites/default/files/styles/full/public/2019-06/files-paper-stack.jpg?itok=3aMDtTgQ)
If you're familiar with navigating your computer in a Linux terminal, then you already know how efficient a terminal can be. Instead of eight clicks to get to a subdirectory of a subdirectory in a directory, you can type one command and all but teleport straight to your destination.
For those who want to polish their skills, or are hesitant to let go of their GUI, this article demonstrates how to manage and interact with files from your terminal. Unlike traditional tutorials that focus on teaching you commands, this article takes a task-based approach, teaching you how to do ordinary things and providing you with the relevant commands as you go.
Create a file
It’s helpful to have files to manage as you follow along. There are plenty of files already on your system, but many of them are important and best left out of practice sessions. Instead, you can create your own test files.
The echo
command is a simple program that echoes whatever argument you provide. For example:
$ echo hello
hello
You can leverage shell redirects (>
) to send the output of nearly any command (echo
included) into a file. The first version of the redirects below overwrites any content currently in a file, while the second appends new content to the end of the file. If the redirect file doesn’t exist, the shell creates it. Try this:
$ echo hello > ~/Documents/hello.txt
$ echo world >> ~/Documents/hello.txt
With the file hello.txt
now located in your Documents
folder, open it in a text editor to see the results. The file contains two lines, reflecting your echo statements.
Generally, it's safer to use two redirects as done above, instead of just one, since one redirect overwrites content. For example, the following does something different than the previous series of commands:
$ echo hello >> ~/Documents/world.txt
$ echo world > ~/Documents/world.txt
Open the world.txt
file in a text editor to see the result.
Move a file
![Moving a file](/sysadmin/sites/default/files/inline-images/file-move_0.png)
In a GUI, open the folder containing the file. Open a second window to the folder you want to move the file to. Drag and drop the file from the first window to the second.
At the command line, the mv
command moves a file from one place to another.
As long as you know where you want to take a file from and where you want to move it to, you can send files flying all over the place, no matter where you are in the filesystem. It’s a huge time-saver compared to navigating through your files in a series of windows to locate the one you want, then opening a new series of windows to get where you want that file to go, and then moving the file.
Suppose you downloaded a Git cheatsheet using a web browser. The typical default location for downloads is, understandably, your ~/Downloads
folder—the ~
refers to the account you're currently logged into's home directory. The following example shows how you to move this file from ~/Downloads
to your collection of cheat sheets at ~/Documents/Cheatsheets
:
$ pwd
/home/seth
$ ls ~/Downloads
git-cheatsheet.md
$ ls ~/Documents
Cheatsheets
$ mv ~/Downloads/git-cheatsheet.md ~/Documents/Cheatsheets
$ ls ~/Documents/Cheatsheets
bash-cheatsheet.md
git-cheatsheet.md
markdown-cheatsheet.md
When using filesystem commands, you can use absolute (exactly where to operate in the filesystem) file paths or relative file paths (where to operate in the filesystem relative to where you are.) For example, if you're moving a file from ~/Downloads
to ~/Documents/cheatsheet
while your terminal prompt is inside ~/Documents/cheatsheet
, you can use the relative notation since it's shorter. To do so, use a dot (.
) to indicate that the file should move to wherever your terminal prompt is currently located, as shown below:
$ pwd
/home/seth/Documents/Cheatsheets
$ mv ~/Downloads/git-cheatsheet.md .
$ ls
bash-cheatsheet.md
git-cheatsheet.md
markdown-cheatsheet.md
By default, the mv
command does not ask whether you want to overwrite a file with the same name as the one you are moving. To protect yourself from this, use mv --interactive
(or mv -i
), which forces mv
to request permission before overwriting an existing file that has the same name as this new one.
Rename a file
![Rename a file](/sysadmin/sites/default/files/inline-images/file-rename.png)
In a GUI, open a window and find the file you want to rename. Click on the file's name or right-click on it to select the rename option. Enter a new name.
At the command line, the mv
command also "moves" a file from itself to itself, with a new name.
One way to rename a file in Linux is by using the mv
command, which can "move" a file from itself to itself, but with a new name. For example:
$ ls
ipv4.list
$ mv ipv4.list ipv6.list
$ ls
ipv6.list
Because both renaming and moving use the same command, you can actually combine them into one action. To move a file from one folder to another, and also rename it in the process, provide the new file name in your move command like so:
$ mv ~/Downloads/ip-address.txt ~/Documents/assets/ipv4.list
$ ls ~/Documents/assets/
ipv4.list
Copy a file
![Copy a file](/sysadmin/sites/default/files/inline-images/file-copy_0.png)
In a GUI, open a series of windows to find the file you want to copy, then open a second window to reach the new destination. Press a modifier key and drag a file from one window to another.
At the command line, the cp
command makes a copy of a file and its contents.
The cp
command works exactly like mv
, except that it duplicates the file's contents instead of moving the file from one location to another. For example:
$ ls ~/Downloads
git-cheatsheet.md
$ ls ~/Documents
Cheatsheets
$ cp ~/Downloads/git-cheatsheet.md ~/Documents/Cheatsheets
$ ls ~/Downloads
git-cheatsheet.md
$ ls ~/Documents/Cheatsheets
bash-cheatsheet.md
git-cheatsheet.md
markdown-cheatsheet.md
As with the mv
command, the cp
command by default overwrites any document with the same name in the destination folder. To avoid that behavior, use cp --interactive
(or cp -i
) like this:
$ cp --interactive ~/Downloads/git-cheatsheet.md ~/Documents/Cheatsheets
cp: overwrite ‘./git-cheatsheet.md’? n
To copy a file into the same directory, give it a new name during the copy operation. For example:
$ cp ~/Downloads/git-cheatsheet.md ~/Documents/Cheatsheets/git-cheatsheet_local.md
$ ls ~/Downloads
git-cheatsheet.md
$ ls ~/Documents/Cheatsheets
bash-cheatsheet.md
git-cheatsheet.md
git-cheatsheet_local.md
markdown-cheatsheet.md
Try copying some of the files you've created for this exercise, and renaming them as needed. If you want your shell to provide you with feedback, add the --verbose
(or -v
) option to your command.
Copy a folder
Folders don't really exist in the Linux filesystem, at least not in the way you imagine them. A folder is just a directory listing of all the files it "contains." To copy a folder, use cp --recursive
(or cp -r
), which copies all of the files in a folder as well as the folder designation itself. Here's an example:
$ ls --classify
people/
$ cp people ~/Documents
cp: omitting directory 'people/'
$ cp --recursive people ~/Documents
$ ls --classify ~/Documents
people/
Create a folder
![New Folder](/sysadmin/sites/default/files/inline-images/folder-new_1.png)
In a GUI, open a series of windows to reach the new folder's location, then click a button or menu to create a new folder. Create a folder anywhere in your file system with the mkdir
command.
At the command line, to create a folder anywhere in your file system, use the mkdir
(make directory) command. You can create a folder in your current filesystem location just by providing a name. For example:
$ mkdir newfolder
$ ls
newfolder
You can create a folder in any other filesystem location by specifying the where it should go using an absolute or relative file path. This example uses the absolute path:
$ mkdir ~/Documents/newfolder
$ ls --classify ~/Documents
Cheatsheets/
people/
newfolder/
Create a shortcut
![Symlink](/sysadmin/sites/default/files/inline-images/symlink_0.png)
In a GUI, find a file you need a shortcut to and select the option to create a shortcut (or link or alias, depending on the OS) from a contextual menu.
The command line has even more to offer here. One of the most powerful features computers offer is the ability to have a file or folder in more than one place at once. If you make two copies of a paper file or folder, you're in danger of making changes to one and forgetting to make the same change to the other. This issue isn't a problem with symbolic linking (symlink for short) in Linux. To create a shortcut, use the ln
command as you see below (you can use ln -s
instead of ln --symbolic
):
$ ls ~/Documents
oncall-schedule.txt
$ ln --symbolic ~/Documents/oncall-schedule.txt ~/Desktop/oncall-schedule
Should you forget which file is a symlink and which is the actual file, use the file
command. For example:
$ file ~/Desktop/oncall-schedule
/home/seth/Desktop/oncall-schedule: symbolic link to `/home/seth/Documents/oncall-schedule.txt'
To identify symlinks at a glance, you can invent your own convention, such as a naming scheme for symbolic links that includes the word link
, as in this example:
$ ls ~/Documents
oncall-schedule.txt
$ ln --symbolic ~/Documents/oncall-schedule.txt ~/Desktop/oncall-schedule.link.txt
Conclusion
Switching to the Linux command line to manage your filesystem can take some getting used to, but it's worth the effort. The further you dig into the command line's capabilities—especially when dealing with your filesystem—the faster you'll get things done. Start by practicing these basic techniques, and then dig deeper.
![Author’s photo](/sysadmin/sites/default/files/styles/user_picture_square/public/pictures/2021-06/sethKenlon-headshot.jpg?itok=OyoGyrQS)
Seth Kenlon
Seth Kenlon is a UNIX geek and free software enthusiast. More about me