Copy a File in Linux : Use the Options to Make Things Easier
*Linux is a contemporary, free-operating system based on the UNIX standards. Over the last two decades, it has been adopted in many ways, on different platforms, and consequently has many flavors. Though through them all, it maintains its powerful essence as a reliable operating system.
In this tutorial, we’ll walk you through the various ways of copying a file in Linux. Yes, there are many options. But they are simple to learn and powerful enough, for you to want to use them.
If you are new to Linux, we highly recommend you take a simple, effective crash course on Linux first. This will help you get a feel of the basic framework and concepts we will use in the tutorial.
Let’s begin with the basics.
How files are created in Linux
When you create a file in Linux, a few different kinds of information gets stored along with it. These include the user name, file type, time of creation, access permissions and a few more. There are two properties that you need to know – inode number and file name. The inode number denotes the actual location of the data stored in the file in the hard disk partition. The file name is actually the human readable pointer to the inode number and is a link to it. The file name is created in the directory structure as specified by the user.
Simple copy: copying to a different name, in the same folder
The copy command in Linux, has been lovingly abbreviated as “cp”. It is one of the basic utilities of Linux, and part of each Linux variant. Its job is to create a copy of whatever file system element you tell it to. It can copy a text file or executable program (binary) file or even an entire directory. It can be used to generate a backup file or to create a backup copy, just in case you want to experiment with the file.
Here is the general syntax of copy command-
> cp [-options] source destination
Let’s assume our source filename is source.txt and our target file name is target.txt.
So the above command becomes
> cp source.txt target.txt
This will create a copy of source file in the same location by a name called “target”.
To verify this, you can try out the ‘ls’ command, to show you the contents of the directory
> ls source.txt target.txt
Copy from a one directory to another
Now let’s say you want to copy a file from one location to another, with the same name. Then the syntax will be-
> cp dir1/source.txt dir2/source.txt
Here, we copy source.txt from dir1/ to dir2/. Alternatively you take a little shortcut and use
> cp dir1/source.txt dir2/.
Linux interprets the ‘.’ to mean “use the same file name”.
To confirm, use the ‘ls’ command again.
> ls dir2/ source.txt
Copy a file in interactive mode
Sometimes, you may common file names in different folders, and not remember it. For example you may files called readme.txt in multiple folders. You name it that way so that whoever goes to that folder, reads it first, to get instructions. Now over time, you may not remember which folders specifically have it, and which don’t. How do you make sure you don’t override it while copying? Going through each folder to find it, can be quite painful. Linux has a simple way – it can prompt you. Try the following command
> cp -i readme.txt dir2/.
This way, if dir2/ already has a readme.txt, Linux will prompt you with a question “Do you want to copy or not”. You can give a simple “Y” or “N” option from keyboard
Force a copy
Sometimes it may happen that you are not allowed to copy the file from source to target. This may happen due to security reasons or permissions or may be because the file is already present on the target location. This is exactly opposite scenario when compared to an interactive mode copying.
In this case we can leverage the –f option of cp command also known as “Force copy”. The syntax will be-
> cp -f readme.txt dir2/.
This option first removes the file and creates a new file with same name, without prompting for confirmation regardless of its permissions.
Just in case you have scheduled background scripts running, or multiple users accessing the same file system, Linux has a rather nifty option ‘–u’ which copies a file only in 2 cases.
a. The file at source is newer than the one at target.
b. The file is missing from target location.
The syntax is a simple
> cp -u readme.txt dir2/.
Copying multiple files
Let’s you have multiple files (source.txt, source1.txt, source2.txt and so on up to sourcen.txt), and you want to copy some of them. You can either manually type the names of the files to be copied or you can use a wildcard. Let’s see how.
If you want to selectively copy just a few of the files, it is best to list them all by name
> cp source1.txt source2.txt dir2/.
Note the use of the ‘.’. This ensures we copy the files over to the new directory with the same name. Again, let’s check whether this actually worked. Remember the ‘ls’ command?
> ls source1.txt source2.txt
If you have more than a handful of files, typing out all the names can be quite painful. As always, Linux has a handy way to do it. Linux lets you use wildcard characters to specify the file name. Let’s see how.
> cp source*.txt dir2/.
This is the short form to tell Linux to copy source1, source2…sourcen , ie all files starting with ‘source’ over to dir2/. Let’s look at another command
> cp source.txt readme.txt dir2/.
Can you guess what it does? Yes, it copies all the “source” files along with readme.txt. Let’s take it a step further. How will you copy ALL files over to a new folder? Try it out yourself before looking at the answer.
> cp * dir2/.
Like we mentioned at the start, Linux is a pretty powerful operating system. It gives the user a very level of granularity to get things done. We hope this tutorial helped you understand the basic copy function, and showed you how to master the Linux command line.
Top courses in Linux
Linux students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.