> 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.
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: