This is a series that 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.
All articles in this series:
|Linux For Starters|
|Part 1||What is Linux? Why use Linux? What do I need?|
|Part 2||Choose a Linux distribution meeting your specific needs and requirements.|
|Part 3||Make a bootable Ubuntu USB stick in Windows.|
|Part 4||We show you how to install Ubuntu 21.04 on your hard disk.|
|Part 5||Things to do after installing Ubuntu.|
|Part 6||Navigating your way around the Desktop.|
|Part 7||Updating the system, install new software.|
|Part 8||Open source replacements for proprietary Windows desktop software.|
|Part 9||Get started with the power and flexibility of the terminal.|
|Part 10||We cover the basics of files and permissions.|
|Part 11||Getting help from your system.|
|Part 12||Learn all about the file system.|
|Part 13||Manipulating files from the shell.|
|Part 14||Maintain your system with these simple tips.|
|Part 15||Managing users on your system.|
|Part 16||Explore different desktops to GNOME 3.|
|Part 17||Gaming on Linux.|
|Part 18||Protect your privacy with this guide.|