How to copy and rename files in Linux

There are more ways to copy and rename files on Linux than just cp and mv. Try some commands and other ways suggested below.

There are more ways to copy and rename files on Linux than just cp and mv. Try some commands and other ways suggested below. They can surprise you and save you a lot of time.

Linux users for decades have used simple cp and mv commands to copy and rename files. The cp and mv commands are the first commands that most of us learn and use everyday. But there are techniques, handy variants and some other commands to rename the files, as well as provide certain options.

  1. Basic Linux commands everyone needs to know

How to copy files in Linux

First, think about why you want to copy the file. You may need the same file in another location or you may want to create a copy before you edit the file to use in case you need to revert to the original file. The obvious way to do that is to use a command like ' cp myfile myfile-orig '.

However, if you want to copy a large number of files, this method will not be suitable. Better alternatives are:

  1. Use the tar command to create an archive of all the files you want to back up before you start editing them.
  2. Use the for loop to make backups easier.

Tar options are very simple. For all files in the current directory, you will use the following command:

 $ tar cf myfiles.tar * 

For a group of files that you can define using templates, you will use the following command:

 $ tar cf myfiles.tar * .txt 

In the first case you will create a file myfiles.tar , containing all the files in the directory. In the second case, the file myfiles.tar will contain all the files with the .txt extension.

Using the following loop will help create backups with modified names:

 $ for tập tin trong * 
> by
> cp $ file $ file-orig
> done

When you back up a file and the file has a very long name, you can rely on using the tab command to use the Filename completion feature by pressing the tab key after entering the file name and using it. This syntax to add "- orig " to the copy.

 $ cp file-with-a-very-long-name {, - orig} 

After that, you have 2 file names as follows:

xxxxxxxxxxxx and xxxxxxxxxxxx-orig.

How to rename files in Linux

The traditional way to rename a file is to use the mv command. This command will move a file to another folder, change its name and leave it in a certain location.

 $ mv myfile / tmp 
$ mv myfile notmyfile
$ mv myfile / tmp / notmyfile

But now we have many other rename commands to make some important file name changes. The secret to using the rename command is to get used to its syntax, but if you know a little bit about the perl programming language, you may not find it complicated at all.

This is a very useful example. Suppose you want to rename the files in the folder to replace all uppercase letters in lowercase. In general, you won't find many files that are uppercase on a Unix or Linux system, but sometimes this can happen. Here is an easy way to rename them without using the mv command for each file. The parameter / AZ / az / indicates the rename command to change any letter in AZ range to the corresponding letters in az.

 $ ls 
Agenda Group.JPG MyFile
$ rename 'y / AZ / az /' *
$ ls
agenda group.jpg myfile

You can also use the rename command to delete the file extension. You may feel uncomfortable with text files with .txt tags . Simply remove them with a command like this:

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

Now imagine you change your mind and want to reset the file extension. No problem. Just change the command. The trick is to understand that " s " before the first slash means " substit '. Between the first two slashes are what you want to change. Between the second and third slashes is what you want to change to. So, $ represents the end of the file name and we will rename it to '.txt'.

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

You can also change other parts of the file name. Leave the rule s / old / new /.

 $ ls 
draft-minutes-2018-03 draft-minutes-2018-04 draft-minutes-2018-05
$ rename 's / draft / approved /' * minutes *
$ ls
approved-minutes-2018-03 approved-minutes-2018-04 approved-minutes-2018-05

Note: In the examples above, when we use an s as in " s / old / new / ", we will replace part of this name with another name. When using y , you are translating (replacing characters from one scope to another).

There are many options for copying and renaming files in Linux. Find a plan that works best for you. Good luck!

See more:

  1. Search for files and directories in Linux using the command line interface
  2. Learn the file system and folders on Linux operating systems
  3. 7 commands to manipulate the most basic files and folders everyone must know
Update 26 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile