Linux for Starters

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

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.


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

2 comments

Share your Thoughts

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