Home > Tutorials > Linux Server Tutorials > The BASH Shell: Copy, Move & Delete Files on Linux Servers

The BASH Shell: Copy, Move & Delete Files on Linux Servers

Any webmaster will inevitably find themself staring at a terminal screen, tasked with a job of performing some management/maintenance work on a Linux based server. Most of the time you’ll find yourself doing basic tasks – like moving files, removing unnecessary directories, and renaming other ones. All of these things can be accomplished using a few simple commands – which we will cover within this linux tutorial.

How To Copy Files on Linux

Working with files inside Linux is pretty straightforward once you get the hang of it. We’ll start off by learning the copy command, or cp.

To copy a single file, called file1 to another directory, called new_folder, we use the following command:

cp file1 new_folder

If we had multiple files that we wanted to copy to another directory, our cp command would look like this:

cp * new_folder/

Lastly let’s say that we had multiple files, and multiple folders, and we wanted to copy everything over to our new folder while retaining the current tree structure. For this we would use the -r option, for recursive.

cp -R * new_folder/

How To Rename & Move Files on Linux

Linux makes do with a single command to move & rename files – the mv command. In theory – renaming works by simply making a new folder with the desired name, and moving all the files to it. Let’s say we had a folder called Expenses_2016 and we wanted to rename it to Old_Expenses, this would be our syntax:

mv Expenses_2016 Old_Expenses

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

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

How To Delete Files & Folders On Linux

Lastly we’re on to removing/deleting files. This is handled using the rm command. If we wanted to remove a file, we can go:

rm old_file.txt

You can also delete an entire directly using the same command. However you will have to use the -r option if that directory has other folders inside of it.

rm -r Old_Directory

Take note that Linux doesn’t have a recycle bin of any sort, and as such when you delete files – they’re gone. To help minimize mistakes you’ll notice that Linux will prompt you before deleting any file. If you’re going through a large directory, with many sub-directories, and trying to delete everything – you’re going to get pretty annoyed of all those prompts. You can avoid this by adding the -r and f options together:

rm -rf Old_Directory

For more advanced Linux server tutorials, like setting up alias commands, check out our full tutorial list.

About Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Check Also

Separating the Chaff from the Wheat: SEO Experts Versus Charlatans

The world is full of charlatans. I don’t like writing that, but it’s true. As ...

Like every other website, this site uses cookies to analyze our traffic. Cookies may also be utilized by our advertisers and partners. By using this website you agree to the use of said cookies. More Information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings, continuing to navigate past this message, or you click "Accept" below then you are consenting to the use of cookies on the Rapid Purple website.

Close