How to Copy, Move, and Delete Files in Linux - The Ultimate Guide (2024)

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.

How to Copy, Move, and Delete Files in Linux - The Ultimate Guide (2024)

FAQs

How do I copy and move files in Linux? ›

You can copy files by right-clicking on the file and selecting "Copy", then going to a different directory and selecting "Paste". For my terminal friends, you can also perform file copy-paste operations without leaving the terminal. In a Linux-based terminal, you do this using the cp command.

How do I copy and delete a file in Linux? ›

3 Commands to Use in the Linux Command Line:
  1. mv: Moving (and Renaming) Files. The mv command lets you move a file from one directory location to another. ...
  2. cp: Copying Files. ...
  3. rm: Deleting Files.
Mar 25, 2023

How do I mass move files in Linux? ›

To move multiple files, you should list them all after mv and before the destination. Also you can utilize the question mark ( ? ) wildcard to represent a single character when moving files with similar names, such as 'file1. txt' and 'file2.

How do you copy and overwrite existing files in Linux? ›

-f or --force : This option will force cp to overwrite any existing files in the destination, even if they cannot be opened or removed. This option will also ignore any -n option that is used before it. For example, cp -f source. txt destination.

How to delete files in Linux? ›

You can quickly and easily delete a single file with the command “rm” followed by the file name. With the command “rm” followed by a file name, you can easily delete single files in Linux. With the command “rm” followed by a file name, you can easily delete single files in Linux.

What Linux command moves files? ›

Use the mv command to move files and directories from one directory to another or to rename a file or directory. If you move a file or directory to a new directory without specifying a new name, it retains its original name.

How to copy multiple files at once in Linux? ›

To copy multiple files, you can use the command: cp file1 file2 file3 destination_directory. Q. How do I copy a directory recursively in Linux? To copy a directory and all its contents recursively, you can use the command: cp -r source_directory destination_directory.

How do I move everything in Linux? ›

The 'mv' command to move files does support wildcard characters. Simply use mv folder1/folder2/* <destination> . This command will move all files in folder1/folder2 to your specified destination.

How do you copy everything in a file in Linux? ›

Step-by-step process to copy a file using the cp command
  1. Step 1: Open the terminal window in your Linux operating system.
  2. Step 2: Use the cp command to go to the directory containing the file you wish to copy.
  3. Step 3: Once you are in the current folder where the file is located, use the cp command to copy the file.
Mar 6, 2023

What is the command for copy file in Linux? ›

The `cp` command is an essential tool which is used for copying files or groups of files and directories in Unix-Like operating systems. If we talk about its syntax it takes at least two filenames in as an argument (source and destination).

How do I copy files from one Linux machine to another? ›

The Linux SCP command enables secure file copying between systems through encrypted SSH, offering a more efficient and safer alternative to FTP. Its syntax involves options, system addresses, file location, and target path, with address and directory omission for local transfers.

How do you transfer files in Linux command line? ›

SCP (Secure Copy Protocol) is a network protocol that securely copies files/folders between Linux (Unix) systems on a network. To transmit, use the scp command line utility, a safer variant of the cp (copy) command.

How do I move files in the current directory in Linux? ›

The mv command by default does exactly as it's told: it moves a file from one location to another. Should a file with the same name already exist in the destination location, it gets overwritten. To prevent a file from being overwritten without warning, use the --interactive (or -i for short) option: $ mv -i example.

How do I copy a file in Linux to another directory? ›

To copy a file to the same directory on Linux, use cp with the name of the source file and the name of the copy. The cp command can also be used to copy the file to a different directory, to copy multiple files, or to recursively copy entire directories.

What is the mv and cp command in Linux? ›

mv moves or renames a file or directory, while cp copies a file or directory. mv deletes the source file or directory after moving it, while cp keeps the source file or directory intact. mv does not change the inode number of the file or directory, while cp creates a new inode number for the copied file or directory.

Top Articles
When Does the Crypto Market Open? Does It Close?
Forex Market News & FX Forecast
Dte Outage Map Woodhaven
80 For Brady Showtimes Near Marcus Point Cinema
Phone Number For Walmart Automotive Department
Activities and Experiments to Explore Photosynthesis in the Classroom - Project Learning Tree
5 Bijwerkingen van zwemmen in een zwembad met te veel chloor - Bereik uw gezondheidsdoelen met praktische hulpmiddelen voor eten en fitness, deskundige bronnen en een betrokken gemeenschap.
Autobell Car Wash Hickory Reviews
The Best English Movie Theaters In Germany [Ultimate Guide]
Mndot Road Closures
Encore Atlanta Cheer Competition
Strange World Showtimes Near Amc Braintree 10
Craigslist/Phx
Gt Transfer Equivalency
Sams Gas Price Fairview Heights Il
Unit 1 Lesson 5 Practice Problems Answer Key
Lqse-2Hdc-D
Bad Moms 123Movies
Condogames Xyz Discord
Truck Trader Pennsylvania
Craftology East Peoria Il
Tamilrockers Movies 2023 Download
Talbots.dayforce.com
H12 Weidian
Mc Donald's Bruck - Fast-Food-Restaurant
Sea To Dallas Google Flights
Noaa Duluth Mn
Homeaccess.stopandshop
Best Transmission Service Margate
Highmark Wholecare Otc Store
3 2Nd Ave
Directions To Nearest T Mobile Store
Wsbtv Fish And Game Report
Craigslist Northern Minnesota
Puffin Asmr Leak
Ugly Daughter From Grown Ups
The Ride | Rotten Tomatoes
Tendermeetup Login
Glossytightsglamour
Facebook Marketplace Marrero La
The Transformation Of Vanessa Ray From Childhood To Blue Bloods - Looper
140000 Kilometers To Miles
This 85-year-old mom co-signed her daughter's student loan years ago. Now she fears the lender may take her house
Aurora Il Back Pages
manhattan cars & trucks - by owner - craigslist
Smite Builds Season 9
6576771660
Advance Auto.parts Near Me
Optimal Perks Rs3
Uncle Pete's Wheeling Wv Menu
Predator revo radial owners
Latest Posts
Article information

Author: Dr. Pierre Goyette

Last Updated:

Views: 6548

Rating: 5 / 5 (70 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Dr. Pierre Goyette

Birthday: 1998-01-29

Address: Apt. 611 3357 Yong Plain, West Audra, IL 70053

Phone: +5819954278378

Job: Construction Director

Hobby: Embroidery, Creative writing, Shopping, Driving, Stand-up comedy, Coffee roasting, Scrapbooking

Introduction: My name is Dr. Pierre Goyette, I am a enchanting, powerful, jolly, rich, graceful, colorful, zany person who loves writing and wants to share my knowledge and understanding with you.