How to Copy, Move and Rename Files in Linux (Step-by-Step Guide)

There is much more to copying and renaming files on Linux rather than cp and mv. Try out some commands and strategies to save your time.

For this purpose, users have been using cp and mv for decades. These were some of the few commands we learned and use every day, similarly, there are other techniques, handy variations, and other commands used for this purpose that provide unique options.

Copying Files in Linux – cp command

The need to copy a file? You may need it in another location or want to edit it and be sure that you have a backup in case it needs to be reverted back. To do this use commands like:

cp dekifile dekifile-new

Want to copy a large number of files? That strategy may get old real fast.

Alternatives are:

  • You can use tar to create an archive of all files you want to keep a backup of before editing them.
  • You can use for loop to make backup copies easy.

The first option is quite straight forward. For all file in the current directory, use the command:

$tar cf yourdatafileexample.tar *

For a group of files that can be identified with a pattern, use the command:

tar cf myfiles.tar *.txt

In every case; you end up with myfiles.tar file which carries files in directory or all files with .txt extension.

The easy loop allows users to make backup copies with modified names:

$ for file in *

< do

< cp $file $file-orig

< done

If you are backing up a file with a long name then the user can depend on using the tab command in order to use filename completion (press tab once you have entered enough letters to identify the file uniquely) and use syntax like this in order to append “-orig” to copy.

$ cp your-long-name-file-which-is-not-a-good-example{, -orig}

You then have file-with-a-very-long-name and a file-with-a-very-long-name file-with-a-very-longs-name-orig.

Renaming and Moving Files in Linux

Use the mv command for this purpose. This is used to move file towards a different directory, change the name and leave it in place, or in some do both.

$ mv myfile /tmp

$ mv myfile notmyfile

$ mv myfile /tmp/notmyfile

We can also use the rename command, trick to use it is to get used to its syntax however if you know some Perl, you may not find it tricky.

Let us see some examples. If you wanted to rename some files in a directory in order to replace all of the upper case letters with lower ones. Generally, you don’t find such files with capital letters on Unix or Linux system, but you can. Following is an easy way to do this without using the mv commands for each one. The /A-Z/a-z/ specification informs the command to change any letters which are ranging from A-Z with a-z.

$ ls
DekiSoft-Linux-Tutorial.JPG MyFile
$ rename 'y/A-Z/a-z/' *
$ ls
DekiSoft-Linux-Tutorial.JPG MyFile

Use this to remove file extensions; It might be that you get tired of seeing text files with the same .txt extensions. Just remove them simply- with one command:

What if you want to put the extensions back? Don’t worry you just need to change the command for this. The trick is that you need to understand the “s” before the first slash means “substitute”. What lies between the first two slashes is what needs to be changed, and what lies between the second and third slashes is what we want it to be changed to. $ represents the ends of the filename which we are changing to .txt.

$ ls
agenda.txt  notes.txt  weekly.txt
$ rename 's/.txt//' *
$ ls
agenda  notes  weekly

Other parts of filenames can be changed as well. For this you need to keep the s/old/new rule in your mind:


$ ls
old-file-2020  old-file-2019  old-file-2018

$ rename  's/draft/approved/' *file*

$ ls

keep in mind that in the examples that have been explained above, when we use an s as in s/old/new, we are actually adding one part of the name with another. When we use y we are transliterating that is adding characters from one range to another. In Windows, you can use programs like Sensible File Renamer for renaming files or folders in batch.

MV command tips

Use mv command (man mv) for this purpose. This is similar to the other command except it moves it physically from one place to another place in spite of being duplicated.

Common options available with this include:

  • i – Interactive: This is used to prompt users if the file they have selected overwrites an existing file in the destination directory. This is quite a good option as with this option in cp they shall be given chance to make sure to want to replace an existing file.
  • F – Force: This is used to override the interactive mode and move without prompting. Unless and unless the user knows what he is doing, such as option doesn’t play nice thus be careful while using this.
  • v – Verbose: This is used to show a list of all the files that are being moved.

Use the following command, if you wish to move a file out of your directory to another:

Mv dekisoft-shaheer.txt /home

Or

Mv dekisoft-shaheer /home/newuser/home/newuser/tigger by using absolute pathnames.

Last Words

So this was the simplest guide on how you can copy, rename, and move files in a Linux environment. I have used easy day-to-day words for examples to clear out the concept for you and showed the workings of all basic commands including cp and mv.

Leave A Reply

Please enter your comment!
Please enter your name here