How to Move, Copy, and Delete Files in Linux

Move Copy Delete Files Linux

Aha! Simple, you say. Just go to the graphical interface you’re using for your Linux system. Then you can quickly and easily move the file of your choice from one place to another, copy it, or zap it into nothingness.

Now, what if you wanted to move (or copy or delete) many files at the same time? In particular, what if you wanted to move all files with the same characters at the end of their name, like joe_expenses, cath_expenses, mike_expenses and robin_expenses?

3 Commands to Use in the Linux Command Line:

In the case mentioned above, the Linux command line offers far greater power and efficiency than the GUI. For instance, to instantly seek out and move all of the files above to a subdirectory called budget, your command line instruction would simply be:

mv *_expenses budget

Each of the Linux commands to move, copy, or delete files have options to make it more productive. Read on to find out more.

1. mv: Moving (and Renaming) Files

The mv command lets you move a file from one directory location to another. It also lets you rename a file (there is no separate rename command).

Let’s start with the basic format:

mv joe_expenses JOE1_expenses

In this case, if JOE1_expenses does not exist, it will be created with the exact content of joe_expenses, and joe_expenses will disappear.

If JOE1_expenses already exists, its content will be replaced with that of joe_expenses (and joe_expenses will still disappear).

Additional Options

Options for mv include:

-i for interactive, asks you to confirm if an existing file should be over written.

-f for force, overrides all interactivity and executes the mv instruction without returning any prompts. (You must be sure your instruction is exactly what you want if you decide to apply the -f option.)

-v for verbose, to show the files being moved one by one

2. cp: Copying Files

A basic example of the cp command to copy files (keep the original file and make a duplicate of it) might look like:

cp joe_expenses cashflow

In this example, we copy the joe_expenses file to the cashflow directory, which (because we haven’t specified anything else) is in our login directory.

Additional Options

Options are similar to those for the mv command:

-i for interactive, asks you to confirm if an existing file (perhaps a version of joe_expenses already exists in the cashflow directory) should be over written in the copying process.

-r for recursive, to copy all the subdirectories and files in a given directory and preserve the tree structure.

-v for verbose, shows files being copied one by one. For example:

cp joe_expenses cath expenses cashflow

3. rm: Deleting Files

File deletion is done using the rm (remove) command.

rm joe_expenses

This will delete the joe_expenses file forever (maybe Joe would like that!).

Additional Options

The rm command options include -i (interactive), -f (force), -v (verbose), and -r (recursive).

Like the commands above, it can also be applied to more than one file at a time.

rm joe_expenses cath_expenses

This will remove both of these files.

Using the wildcard character: “*”

rm *_expenses

This will remove joe_expenses, cath_expenses, mike_expenses, and robin_expenses, forever.

Likewise, if you decide you want to remove everything you copied into the cashflow directory above and the directory itself, use:

rm -r cashflow

Linux System Health Warning: Use Caution with These Commands

For each of these commands, the use of the -i (interactive) option is highly recommended, at least in the beginning. This gives you a second chance to spot any unfortunate mistakes.

Similarly, use caution if you apply either -f (force) or -r (recursive), especially if you are also using a wildcard character like “*” to apply the command to several files at once.

Beware of the -r Option!

We’ll say it once and once only. Don’t do this:

rm -r *

This will delete every file and every directory you have.

Happy Moving! (Or Copying Or Deleting!)

May the force of mv, cp, and rm in the Linux command line be with you, but also make sure you avoid any disasters!

Advertiser Disclosure

HostingAdvice.com is a free online resource that offers valuable content and comparison services to users. To keep this resource 100% free, we receive compensation from many of the offers listed on the site. Along with key review factors, this compensation may impact how and where products appear across the site (including, for example, the order in which they appear). HostingAdvice.com does not include the entire universe of available offers. Editorial opinions expressed on the site are strictly our own and are not provided, endorsed, or approved by advertisers.

Our Editorial Review Policy

Our site is committed to publishing independent, accurate content guided by strict editorial guidelines. Before articles and reviews are published on our site, they undergo a thorough review process performed by a team of independent editors and subject-matter experts to ensure the content’s accuracy, timeliness, and impartiality. Our editorial team is separate and independent of our site’s advertisers, and the opinions they express on our site are their own. To read more about our team members and their editorial backgrounds, please visit our site’s About page.