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. 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
4 ★ | 2 Vote

May be interested

  • How to rename, copy and move spreadsheets in ExcelHow to rename, copy and move spreadsheets in Excel
    instructions on how to rename, copy and move spreadsheets in excel. 1. how to change the name of the worksheet (sheet) in excel - method 1: + double-click the worksheet name - when the selected worksheet name is entered, enter the new name of the worksheet: + after entering the name
  • How to copy, delete, move, rename files/folders using PowerShell on Windows PCHow to copy, delete, move, rename files/folders using PowerShell on Windows PC
    you can copy, delete, move or rename your files or folders on your windows pc using powershell.
  • How to copy files larger than 4GB to USBHow to copy files larger than 4GB to USB
    how to copy files larger than 4gb to usb. how if our usb can not copy files larger than 4gb and error. this is a very basic problem on usb when we store. this problem arises from the format of usb. in this article, the software tips will guide you how to copy files larger than 4gb into usb.
  • How to simply copy files from computer to iPhone / iPadHow to simply copy files from computer to iPhone / iPad
    when copying data from computer to iphone device (not jailbreak) via itunes tool, we can only apply to image and video files, and other files like doc, xls, and pdf are not. today, we will share with you two small applications, called documents and playerxtreme media player, in which documents can help you freely copy all different file types to iphone, playerxtreme media player help transfer media files from computer to iphone without plugging in the cord or itunes.
  • How to use the Install command to copy files in LinuxHow to use the Install command to copy files in Linux
    install is a flexible file copying command in linux and macos suitable for professional users. read this article to discover how to use the install command.
  • 4 Fastest Ways to Copy Files in Windows4 Fastest Ways to Copy Files in Windows
    copying files in windows is a familiar operation, but not everyone knows how to optimize to copy data faster. with large files, you may encounter slowness, file too large errors or interruptions. let's learn with free download how to copy files effectively, from shortcuts, and robocopy.
  • How to Select Folders to Sync with Copy on WindowsHow to Select Folders to Sync with Copy on Windows
    having the copy application on your computer allows you to continuously sync the contents of your local copy folder with those of your main copy account. you don't need to go to copy's website to download your files. you're assured that...
  • Instructions on how to rename files in batches very quicklyInstructions on how to rename files in batches very quickly
    usually for batch file renaming we have quite a lot of ways and many tools to support this. in this article, thuthuatphanmem.vn will guide you the simplest and fastest way even without the need for any software to rename the file.
  • How to access Linux files on Windows 10How to access Linux files on Windows 10
    windows 10, april 2019 update brings a long-awaited feature: support for easy access, viewing and even modification of windows subsystem for linux files (wsl) from file explorer or via stream command.
  • Tips for copying files between different user accounts in Windows 10Tips for copying files between different user accounts in Windows 10
    if you share a pc with other people, it's a great idea to create separate user accounts for each person. it's a great way to give people some privacy so that users can store files without worrying about others accessing them.