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.
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.
Pages in this article:
Page 1 – cp
Page 2 – mv
Page 3 – rm
Page 4 – mkdir
All articles in this series:
Thanks. How many parts will this guide be?
We’re not certain of the final number of parts, but there’s plans for quite a few more. And we’re open to suggestions for additional areas that would help a beginner to Linux, or topics already covered where additional material would be welcome.