How to Move, Copy, and Delete Files in Linux

How to Move, Copy and Delete Files in 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:

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:

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:

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:

3. rm: Deleting Files

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

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.

This will remove both of these files.

Using the wildcard character: “*”

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:

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:

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!

PJ Fancher

Questions or Comments? Ask PJ!

Ask a question and PJ will respond to you. We strive to provide the best advice on the net and we are here to help you in any way we can.

  • shishir kaji shrestha

    how do we take a file behind one directory?

    • Karan Singh Rathore

      put your pc or your laptop into a bucket full of water, that will take you behind one directory.

      • Sachin Kanchan

        This is good 😀

    • Injun86

      cd .. will go back one so incorporate that into your cp command in your bash script if you’re making a custom command

  • Jazzy

    if i rm a file that I copied to the tmp directory it does not remove it from the original location does it?

    • Luc

      No, since you made a copy of the file that is completely independent from the original. By the way, the tmp directory gets wiped when you turn off the computer.