Copy and rename files on Linux

In addition to cp and mv, there are many more commands for copying and renaming files on Linux. Try these commands that might surprise you and save you some time.

Linux users have been using the simple cp and mv commands to copy and rename files for decades. These commands are the first ones most of us learn, and probably millions of people use them every day. But there are other techniques, convenience methods, and additional commands that provide some unique options.

First, let's think about why you would want to copy a file. You may need to use the same file in another location, or you may need a copy because you will be editing the file, and you want to make sure you have a backup handy in case you need to restore the original. The obvious way to do this is to use a command like cp myfile myfile-orig.

However, if you want to copy a large number of files, then this strategy may become very old. A better option is:

Use tar to create an archive of all the files you want to back up before you start editing.

Use a for loop to make backup copies easier.

The way to use tar is very simple. For all files in the current directory, you can use the following command:

For a set of files that can be identified by a pattern, the following command can be used:

In each case, the final result is a myfiles.tar file containing all files in the directory or all files with a .txt extension.

A simple loop will allow you to make backup copies with modified names:

When you're backing up a single file and that file happens to have a long name, you can rely on tab-completing the filename (hitting the Tab key after typing enough letters to uniquely identify the file) and appending -orig to after the copy's name.

Then you have a file-with-a-very-long-name and a file-with-a-very-long-name-orig.

Rename files on Linux

The traditional way to rename files is to use the mv command. This command moves a file to a different directory, or changes its name in-place, or both.

But we also have the rename command to do renaming. The trick to using the rename command is getting used to its syntax, but if you know a bit of Perl, you might not find it tricky.

Here is a very useful example. Suppose you want to rename files in a directory by replacing all uppercase letters with lowercase letters. In general, you won't find files with lots of uppercase letters on Unix or Linux systems, but you can. Here's an easy way to rename them without using the mv command for each of them. /AZ/az/ tells the rename command to change any letter in the range AZ to the corresponding letter in az.

You can also use rename to remove file extensions. Maybe you're tired of seeing text files with a .txt extension. Simply remove these extensions - with one command.

Now let's imagine that you have a change of heart and want to change those extensions back. no problem. Just modify the command. The trick is to understand that the s before the first slash means "alternate". The content between the first two slashes is what we want to change, and the content between the second and third slashes is the changed thing. So, $ signifies the end of the filename, we change it to .txt.

You can also change other parts of the filename. Keep in mind the s/old content/new content/ rules.

​Notice in the above example that when we use s in s/old/new/, we replace part of the name with another name. When we use y, we are translating (replacing characters from one range to another).

Summarize

There are many ways to copy and rename files these days. I hope some of these will make your command line life more enjoyable.

Guess you like

Origin blog.csdn.net/weixin_42610770/article/details/129547334