Home » Blog

How to Copy, Move, and Delete Files in Linux – The Ultimate Guide

When working with Linux there will come a time when you will encounter the need to move, copy or delete files and/or directories.

These are three basic activities every operating system distribution affords users.

Thankfully Linux provides a handy set of commands to perform such tasks via the command line. And for the record, the command line is a very powerful utility; it gives any user the power to do more than a graphical user interface can provide.

While these Linux commands are simple, do not be deceived because you can perform powerful and complex move, copy and delete operations with them.

So let’s delve in and take a look at how to use these commands starting with the Linux copy command

Linux Copy Command – cp : Copying Files

When it comes to performing a Linux copy command, Linux provides you with the cp command. With this command, you can instantly copy files and directories in a snap.

The cp follows this basic syntax:

cp [OPTION] SOURCE DEST

So basically, what the above linux copy file command is doing is copying the SOURCE to DEST.

Now let’s check out a simple linux copy command using the “cp command. (Note: when you copy a file on Linux, you are making a duplicate of it.)

cp sample_data.txt data/

As can be observed in the above example, using the cp Linux copy command, we make a copy of the “sample_data.txt” file to the “data” folder. (Note: if the destination folder is non-existent at time of copy, Linux will automatically create it).

[SPECIAL OFFER]: Lightning Fast VPS Hosting

[BENEFITS]:

  • Expert VPS Hosting Provider
  • Root Access to Run Any Software Stack
  • Professional Linux Administrators on Hand
  • 24/7/365 Technical Support

Click here to access this [SPECIAL OFFER]

Linux Copy File / Directory Options

In addition, the copy command in Linux provides for the inclusion of options when executing the command. For example, with the Linux copy file command, we make use of the following options:

-i: the “i” stands for “interactive” and with this option you can specify if existing file (i.e. sample_data.txt) in the destination directory (i.e. data) should be overwritten during the copy process.

-r: the “r” stands for “recursive” and it allows you to copy recursively meaning all sub-directories and files thus preserving the existing structure.

-v: the “v” stands for “verbose” which tells the shell do display copy process as it’s being executed on the screen.

As you can see the Linux copy file command is pretty simple but very powerful. There is so much that you can do with it that is outside of the scope of this post.

Now, let’s move to the next top which on moving files and directories.

Linux Move Command – mv : Moving (and Renaming) Files

Next we’re going to take a look at how to move a file in Linux. Equally, this can be used when moving a directory as well.

Linux provides you with a handy utility for moving and renaming files known as the mv command. This command also known as the Linux move command and is not only used in moving but also renaming a file.

For example:

mv sample_data.txt data/

The above command simply moves sample_data.txt to the data directory. In the event the data directory does not exist, the system will create it and move sample_data.txt there.

And if sample_data.txt is already existing in data, it will be overwritten.

Linux mv Command Options

Just as with the Linux copy command, the Linux mv command allows you to specify options:

-i: the “i” stands for “interactive” and with this option you can specify if existing file (i.e. sample_data.txt) in the destination directory (i.e. data) should be overwritten during the copy process.

-f: the “f” stands for “force” and it disables all interactivity thus running the Linux mv command without prompts. This can be risky so make sure you know what you’re doing when using the -f option.

-v: the “v” stands for “verbose” which tells the shell do display copy process as its being executed on the screen.

Linux Delete File – rm : Deleting Files

You probably have already gotten the idea by now. Just as with the cp and mv commands, Linux provides the rm for deleting files.

This Linux delete file command syntax is as follows:

rm sample_data.txt

Where sample_data.txt is the file being deleted by this Linux delete file command.

[SPECIAL OFFER]: Lightning Fast VPS Hosting

[BENEFITS]:

  • Expert VPS Hosting Provider
  • Root Access to Run Any Software Stack
  • Professional Linux Administrators on Hand
  • 24/7/365 Technical Support

Click here to access this [SPECIAL OFFER]

Linux Delete File Command Options

As with the rest of the other commands, the Linux delete file command provides additional options such as -i interactive, -f force, -v verbose, and -r recursive.

Furthemore, you can apply it move than one file, for example:

rm sample_data.txt no_data.txt my_data.txt, your_data.txt, test.txt

As well as use wildcard character * when working with it, for exmple:

rm *_data.txt

The above will remove sample_data.txt no_data.txt my_data.txt, your_data.txt BUT not test.txt.

The rm Linux delete file command can also be applied to directories.

For example, this command:

rm -r testfolder

will delete the testfolder and everything in it because the -r recursive option is applied.

[SPECIAL OFFER]: Lightning Fast VPS Hosting

[BENEFITS]:

  • Expert VPS Hosting Provider
  • Root Access to Run Any Software Stack
  • Professional Linux Administrators on Hand
  • 24/7/365 Technical Support

Click here to access this [SPECIAL OFFER]

Wrapping Up

Now that you’ve gotten basic understanding on how to copy, move and delete files in Linux, there are few things you should remember:

-i (Interactive) is Your Friend

Using the -i with your commands is a wise approach as it allows you to confirm your selection and avoid unnecessary accidents.

-r (Recursive) / -f (Force) – Use with Caution

In addition, it is prudent to use the -r and -f options with caution.

Equally most important when you’re using wildcard *. For example, can you imagine accidentally running the following command:

rm -r *

Let that sink in a moment.

You’ve just deleted everything!

Therefore, it’s wise to be prudent.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

CALL

1.800.975.6814

SUPPORT

LIVE CHAT

Get up to 60% OFF for signing up to our newsletter!