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.
- 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:
- Use the tar command to create an archive of all the files you want to back up before you start editing them.
- 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:
- Search for files and directories in Linux using the command line interface
- Learn the file system and folders on Linux operating systems
- 7 commands to manipulate the most basic files and folders everyone must know
You should read it
- Is it possible to run .exe files on Linux?
- File Management in Unix / Linux
- How to use pandoc to convert files on Linux
- How to Create ISO Files on Linux
- 4 methods to copy Linux hard drive
- How to create new files in Linux
- How to synchronize files and folders on Linux
- 14 interesting Linux commands in Terminal
May be interested
- How to completely delete a file in Linux so that it cannot be restoredin linux, files can be deleted but can still be recovered. these are things to do when you really want them to disappear.
- How to check open ports in Linuxwhen troubleshooting network problems or trying to improve security for linux computers, you need to know which ports are open. this article will show you some ways to test open ports in linux.
- How to manage user passwords from Terminal in Linuxlike many things on linux, passwords are easily managed directly from the command line. the passwd utility is designed to allow you to quickly and easily access all password-related commands on the system. you can use it to change and manage your password and other user passwords on the system
- PuTTYgen in Linux - SSH key generatorputtygen is a ssh key creation tool for the linux version of putty. it works similar to ssh-keygen tool in openssh.
- 7 ways to run Linux software on Windowslinux users want to run windows software on linux, whereas windows users want to use linux software. even if you are looking for a better development environment, more powerful command-line tools, you can run linux software without removing windows.
- 6 best media server software for Linuxfor linux, media server software options are numerous. the following article will summarize the 6 best media server applications for linux.