Learning

Linux for Starters: Your Guide to Linux – Manipulating Files – Part 13

Last Updated on May 22, 2022

This series offers a gentle introduction to Linux for newcomers.

In Part 9 of this series we introduced the terminal, the shell and examined the four commands available in the shell: Builtins, aliases, external commands, and functions.

In this article we take you through the basics of manipulating files from the shell. Some of these file manipulations are more conveniently done using a graphical file manager. But manipulating files from the shell offers both flexibility and power.

The GNU Core Utilities or coreutils is a package of software containing implementations for many basic tools. This package provides the basic file, shell and text manipulation utilities. Specifically the package provides 96 separate external commands. We only need to cover 4 of them to explain the fundamentals of manipulating files. These commands are cp, mv, rm, and mkdir. Let’s look at each in turn.


cp

The cp program copies files and directories. In its simplest form, it copies a single file to another location:

$ cp path/to/source_file.ext path/to/target_file.ext

The above command copies the file named source_file.ext to the file named target_file.ext. If target_file.ext doesn’t exist before the command is issued it’s created; if it already exists, it’s overwritten.

The command below copies a file into another directory, keeping the filename:

$ cp path/to/source_file.ext path/to/target_parent_directory

The cp command is often useful to backup a file. But there’s so many other ways of using the cp command.

One of the most powerful features of cp is that it lets us create a copy of a collection of files. This copy may be stored on a removable device. We can recursively copy a directory’s contents to another location (if the destination exists, the directory is copied inside it):

$ cp -r path/to/source_directory path/to/target_directory

If you need feedback of how the copying process is going, there’s a verbose mode which shows files as they are copied.

$ cp -vr path/to/source_directory path/to/target_directory

We can copy text files to another location, in interactive mode. This prompts the user before overwriting.

$ cp -i *.txt path/to/target_directory

This last command introduces one of the most powerful features of the shell: the wildcard. In the above command the wildcard is *, which means to match any character. So *.txt means a match is any file with the file extension txt.

There are other wildcards such ? which matches any single character. Using wildcards, it’s possible to construct very sophisticated selection criteria for filenames. We can use these wildcards for mv and rm too.

Page 2 – mv

Pages in this article:
Page 1 – cp
Page 2 – mv
Page 3 – rm
Page 4 – mkdir


All articles in this series:

Linux For Starters: Your Guide to Linux
1What is Linux? Why use Linux? What do I need?
2Choose a Linux distribution meeting your specific needs and requirements.
3Make a bootable Ubuntu USB stick in Windows.
4We show you how to install Ubuntu 21.04 on your hard disk.
5Things to do after installing Ubuntu.
6Navigating your way around the Desktop.
7Updating the system, install new software.
8Open source replacements for proprietary Windows desktop software.
9Get started with the power and flexibility of the terminal.
10We cover the basics of files and permissions.
11Getting help from your system.
12Learn all about the file system.
13Manipulating files from the shell.
14Maintain your system with these simple tips.
15Managing users on your system.
16Explore different desktops to GNOME 3.
17Gaming on Linux.
18Protect your privacy with this guide.
19Access the Windows desktop from Linux using a remote desktop client.
20Set up a virtual machine running Ubuntu as the host and openSUSE as the guest.
21Wine lets you run Windows programs on Linux without emulation.
22Extend your GNOME desktop with extensions and themes.
XUseful Linux commands.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
D Watson
D Watson
3 years ago

Thanks. How many parts will this guide be?