Linux copy file with rename

Copying and renaming files on Linux

There’s more to copying and renaming files on Linux than cp and mv. Try some commands and strategies that might surprise you and save you some time.

Linux users have for many decades been using simple cp and mv commands to copy and rename files. These commands are some of the first that most of us learned and are used every day by possibly millions of people. But there are other techniques, handy variations, and another command for renaming files that offers some unique options.

First, let’s think about why might you want to copy a file. You might need the same file in another location or you might want a copy because you’re going to edit the file and want to be sure you have a handy backup just 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”.

If you want to copy a large number of files, however, that strategy might get old real fast. Better alternatives are to:

  • Use tar to create an archive of all of the files you want to back up before you start editing them.
  • Use a for loop to make the backup copies easier.

The tar option is very straightforward. For all files in the current directory, you’d use a command like:

For a group of files that you can identify with a pattern, you’d use a command like this:

In each case, you end up with a myfiles.tar file that contains all the files in the directory or all files with the .txt extension.

An easy loop would allow you to make backup copies with modified names:

When you’re backing up a single file and that file just happens to have a long name, you can rely on using the tab command to use filename completion (hit the tab key after entering enough letters to uniquely identify the file) and use syntax like this to append “-orig” to the copy.

You then have a file-with-a-very-long-name and a file-with-a-very-long-name file-with-a-very-long-name-orig.

Renaming files on Linux

The traditional way to rename a file is to use the mv command. This command will move a file to a different directory, change its name and leave it in place, or do both.

But we now also have the rename command to do some serious renaming for us. The trick to using the rename command is to get used to its syntax, but if you know some perl, you might not find it tricky at all.

Here’s a very useful example. Say you wanted to rename the files in a directory to replace all of the uppercase letters with lowercase ones. In general, you don’t find a lot of file with capital letters on Unix or Linux systems, but you could. Here’s an easy way to rename them without having to use the mv command for each one of them. The /A-Z/a-z/ specification tells the rename command to change any letters in the range A-Z to the corresponding letters in a-z.

You can also use rename to remove file extensions. Maybe you’re tired of seeing text files with .txt extensions. Simply remove them — and in one command.

Now let’s imagine you have a change of heart and want to put those extensions back. No problem. Just change the command. The trick is understanding that the “s” before the first slash means “substitute”. What’s in between the first two slashes is what we want to change, and what’s in between the second and third slashes is what we want to change it to. So, $ represents the end of the filename, and we’re changing it to “.txt”.

You can change other parts of filenames, as well. Keep the s/old/new/ rule in mind.

Note in the examples above that when we use an s as in «s/old/new/», we are substituting one part of the name with another. When we use y, we are transliterating (substituting characters from one range to another).

Wrap-up

There are a lot of options for copying and renaming files. I hope some of them will make your time on the command line more enjoyable.

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as «USL» (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she’s chasing the bears away from her bird feeders.

Источник

Copy files with renaming

I have a huge file tree. Some files have same name but in different case, e.g., some_code.c and Some_Code.c .

So when I’m trying to copy it to an NTFS/FAT filesystem, it asks me about whether I want it to replace the file or skip it.

Is there any way to automatically rename such files, for example, by adding (1) to the name of conflict file (as Windows 7 does)?

3 Answers 3

Many GNU tools such as cp , mv and tar support creating backup files when the target exists. That is, when copying foo to bar , if there is already a file called bar , the existing bar will be renamed, and after the copy bar will contain the contents of foo . By default, bar is renamed to bar

, but the behavior can be modified:

There are other variants, such as creating numbered backups only when one already exists. See the coreutils manual for more details.

to find possible candidates, and mcopy showed up.

shows a promising option -D clash-option isn’t that cool? But not so cool — it isn’t described. But there are some hints to mtools.dvi, which I searched on my system, without success, and via google, without success, but then, with google, I searched directly for mcopy clash-option and found this site.

I made a short test

to tests for autorename and targetdir a — instead of autorenaming it asked me for every file to ignore or override, that stupid s. .

My version is mtools-4.0.10 and the help page is from 1996 — 15 years old. Should we really lost some features, meanwhile?

I would split the work into two steps:

  • Make a short function, which generates a unique name for a file, if that name is occupied.
  • Run find , and execute that script for every file you wish to copy.

Shall we assist in this approach? 🙂

Here is a script, to autorename files:

and this is my test invocation:

Note: -maxdepth, -name and -type where used to restrict the number of affected files dramatically. I didn’t test the script for deeper file structures, nor for blanks in filenames and other, funky characters like linefeed, pagefeed and so on.

I used .1 because it doesn’t make trouble in most commands, while a ( and a ) often need masking.

Источник

Linux copy file with rename

By now, you’ve learned a little about the structure of the filesystem; and you’ve learned how to create files and directories.

But just because you know how to create files and directories doesn’t mean that you’re stuck with the changes you’ve made. What if you want to rename and/or move files and directories?

Let’s start with the copy command.

Like so many Linux features, you have a variety of options from which to choose when you want to manipulate files and directories. You can also use wildcards when you’re copying, moving, or deleting files and directories.

Basically, the copy command is not much more complex than typing:

so to copy the file sneakers.txt to the directory tigger in your login directory, just type:

cp sneakers.txt tigger

Notice that you also used relative pathnames to copy the file. You can use both relative and absolute pathnames with cp . Our login directory is the parent of the directory tigger ; meaning that tigger is one directory down from ours.

Read the cp man page ( man cp ) for a full list of the options available with cp . But among the options you can use with cp are:

-i — interactive. Prompts you to confirm if the file is going to overwrite a file in your destination. This is a handy option because it can help prevent you from making mistakes.

-r — recursive. Rather than just copying all the files and directories, copies the whole directory tree, subdirectories and all, to another location.

-f — force. Copies without prompting you for confirmation that the file should be overwritten. Unless you’re sure you want to force the copy, you probably don’t want to make friends with this option right now.

-v — verbose. Will show the progress of the files being copied.

Just by using cp alone, you won’t see much when the command is executed. Using an option, such as -i , can make the process a little more useful, because if you want to copy a file to a location that already has a file with the same name, you’ll be asked first if you really want to overwrite — meaning replace — the file that’s already there.

Remember that among your options is -f (force), which can overwrite files without asking you if you’re certain. Make sure, when you use the force option, that you really want to overwrite a file.

Now that we have the file sneakers.txt in the tigger directory, let’s use cp -i to copy the file again to the same location.

[newuser@localhost newuser]$ cp -i sneakers.txt tigger cp: overwrite ‘tigger/sneakers.txt’?

To overwrite the file that’s already there, press Y and then Enter . Don’t want to overwrite the file? Now is the time to press N and Enter .

To move files, use the mv command ( man mv ), which is similar to the cp command, except that with mv the file is physically moved from one place to another, instead of being duplicated, as with cp .

Common options available with mv include:

-i — interactive. Will prompt you if the file you’ve selected will overwrite an existing file in the destination directory. This is a good option, because like the -i option in cp , you’ll be given the chance to make sure you want to replace an existing file.

-f — force. Overrides the interactive mode and moves without prompting. Unless you know what you’re doing, this option doesn’t play nice; be very careful about using it until you become more comfortable with your system.

-v — verbose. Shows a list of the files being moved.

If you want to move a file out of your home directory and into another directory, you would type:

mv sneakers.txt tigger

or, mv sneakers.txt /home/newuser /home/newuser/tigger using absolute pathnames.

Actually, we’ve already covered half of renaming, because when you copy or move files, you can also rename.

To copy the file sneakers.txt from our login directory to our tigger subdirectory, just type:

cp sneakers.txt tigger

To copy and rename that file from sneakers.txt to piglet.txt , type:

cp sneakers.txt tigger/piglet.txt

To move and rename the file, just substitute mv for cp in the above example.

If you cd to tigger and use ls , you’ll see the file piglet.txt .

If you just want to rename the file and keep its location, just mv in your current directory:

mv sneakers.txt piglet.txt

We talked about creating files with the touch command and by using redirection in Chapter 13 . And we created the directory tigger using mkdir .

But we haven’t discussed how to delete files and directories.

Deleting files and directories with the rm command ( man rm ) is a straightforward process.

Let’s take our new file piglet.txt , and delete it from the tigger directory with the rm command:

What happens if we didn’t really want to get rid of it? Too late! Again, that’s where the -i (interactive) option comes in handy, because it gives a second chance to think about whether we really want to toss the file.

[newuser@localhost newuser]$ rm -i piglet.txt rm: remove ‘piglet.txt’?

You can also delete files using the wildcard * , but be careful, because you can easily delete files you didn’t intend to throw away.

To remove a file using a wildcard, you would type:

You can also remove more than one file in one command, as in:

rm piglet.txt sneakers.txt

Options for removing files — and directories — include:

-i — interactive. Prompts you to confirm the deletion. This is good.

-f — force. Overrides interactive mode and removes the file(s) without prompting. This might not be good, unless you know exactly what you’re doing.

-v — verbose. Shows a listing of files as they’re being removed.

-r — recursive. When removing directories, will remove all of the files and the subdirectories of the specified directory. This can also get rid of an empty directory.

To remove directories with rm , you must specify the -r option.

For example, if you want to recursively remove the directory tigger you would type:

And if you want to combine options, such as forcing a recursive deletion, you can type:

Don’t be too «forceful»

The rm is a powerful command, and can delete your entire system! If you’re root and you type the simple command rm -rf / you’re sunk — like a snake eating its tail, the command will recursively remove everything on your system.

A safer alternative to using rm for removing directories is the rmdir command. With this command, you won’t be allowed to use recursive deletions, so a directory which has files in it won’t be deleted.

Read the rmdir man page by typing man rmdir to find out more about the command.

Источник

How to Copy, Move and Rename Files and Directories in Linux System

Till now we have seen how to explore the Linux System, the meaning and use of wildcards, and create and delete files and directories in Linux System.

Now let us look at how we can copy, move, and rename these files and directories.

Let’s start with how can we copy files and directories

1. cp command

The cp command stands for copy is used to copy files and directories in Linux System.

The syntax for cp command.

We can simply use the cp command along with the source and destination.

In the above example, we used the command cp file1.txt file2.txt where

cp represents the copy command
file1.txt represents the source file «file1.txt»
file2.txt represents the destination file «file2.txt»

So what if file2.txt did not exist?

If file2.txt exists, it is overwritten with the contents of file1. If file2.txt does not exist, it is created.

Okay but can we do something to check when the file is being overridden?

  • copy file1.txt to file2.txt and prompt if file2.txt is being overridden

In the above example, we use the command cp -i file1.txt file2.txt to copy file1.txt to file2.txt and we used to option -i to make it interactive and provide us a prompt if file2.txt is being overridden.

Okay so now we know how to copy files but what if we need to copy all the contents from directory1 to directory2?

  • copy contents of one directory to another

In the above example, we used wildcard * and created the command cp logs1/* logs2 where

cp represents the copy command
logs1 represents the source directory
logs1/* represents all the contents of the directory logs1
logs2 represents the destination directory

Unlike the case of when we copy a file we need destination directory to exist when we use cp command.

So what if we want that
If the folder does not exist then the folder should be created and then contents should be copied.

In the above example, we used the option -r and created a command cp -r logs1 logs2 where

cp represents the copy command
-r represents recursively
logs1 represents the source directory
logs2 represents the destination directory

Now we know how we can copy a single file/directory but what about multiple files and directories.

The cp command syntax for multiple files and directories

We can simply use the cp command along with all the sources and the destination.

In the above example, we used the commands cp file1.txt file2.txt logs1 where

cp represents the copy command
file1.txt represents the source file file1.txt
file2.txt represents the source file file2.txt
logs1 represents the destination directory logs1

These are the ways we can copy files/directories. Now let’s have a look at how to move files/directories.

2. mv command

The mv command stands for move is used to move files and directories in Linux System.

The syntax for mv command.

We can simply use the mv command along with the source and destination.

In the above example, we used the command mv file2.txt logs1 where

mv represents the move command
file2.txt represents the source file file2.txt
logs1 represents the destination directory logs1

To check whether the file has been moved to the destination directory we used the command ls . logs1 where

ls represents list command
.(Dot) represents the current working directory
logs1 represents the destination directory logs1

What if we want to move one directory to another directory?

  • move directory logs1 to directory logs2

In the above example, we used the command mv logs1 logs2 where

mv represents the move command
logs1 represents the source directory logs1
logs2 represents the destination directory logs2

To check whether the file has been moved to the destination directory we used the command ls . logs2 where

ls represents list command
.(Dot) represents the current working directory
logs2 represents the destination directory logs2

So, what happens if the destination logs2 does not exist?

If the directory does not exist then a new directory is created and then the contents are moved to that directory.

Similar to what we saw with cp command the mv command also overwrites the files if it already exists.

But what if we want to move only those files that are not already present in the destination directory and skip files that are already present?

  • move files that either doesn’t exist or are newer than the existing corresponding files

In the above example, we first checked the content of the directories logs1 and logs2 and found that both logs1 and logs2 contain the file1 and file2.

After that, we used the command mv -u logs1/* logs2 where

mv represents the move command
-u represents update
logs1/* represents the content inside the logs1 directory
logs2 represents the destination directory logs2

What if we want to move all the .txt files to a new folder?

  • move .txt files to a folder named «text_files»

In the above example, we first listed all the contents of the current directory and then used the command mv *.txt text_files where

mv represents the move command
*.txt represents all the .txt files
text_files represents the destination directory text_files

To check whether all the files of type «.txt» have been moved or not we used the ls . text_files command where

. represents the current working directory
text_files represents the destination directory text_files

Now we know how to easily move tons of files within a second just by using our wildcards.

These are the ways we can move files/directories. Now let’s have a look at how to rename a file/directory.

3. mv command for renaming

Hold on, didn’t we just used the mv command to move the files and directories?

Yes the mv command is used for both moving as well as renaming a file/directory.

The syntax for mv command for renaming

To rename a file we just used mv command along with current_name and new_name

In the above example, we used the command mv original new where

mv represents move command
original represents the current_name original
new represents new_name new

Note:- Please be cautious that the new name that we are giving to the file is not the name of already existing file because if the file with new_name already exists then the file will be overridden when we use mv command.

Let’s have a look at how we can rename a directory

In the above example, we used the command mv original_folder/ new_folder where

mv represents move command
original_folder/ represents the current_name original_folder
new_folder represents new_name new_folder

Okay, so that’s all the commands we need to know for Copy, Move, and Rename Files and Directories in Linux System.

I hope you understood the basics of file manipulation in Linux. Please, let me know if there are any questions.

Источник

Читайте также:  Прочистка головки принтера epson windows 10
Оцените статью
Be careful!