Move rename file linux

mv Command in Linux: 7 Essential Examples

mv command in Linux is used for moving and renaming files and directories. In this tutorial, you’ll learn some of the essential usages of the mv command.

mv is one of the must known commands in Linux. mv stands for move and is essentially used for moving files or directories from one location to another.

The syntax is similar to the cp command in Linux however there is one fundamental difference between these two commands.

You can think of the cp command as a copy-paste operation. Whereas the mv command can be equated with the cut-paste operation.

Which means that when you use the mv command on a file or directory, the file or directory is moved to a new place and the source file/directory doesn’t exist anymore. That’s what a cut-paste operation, isn’t it?

mv command can also be used for renaming a file. Using mv command is fairly simple and if you learn a few options, it will become even better.

7 practical examples of the mv command

Let’s see some of the useful examples of the mv command.

1. How to move a file to different directory

The first and the simplest example is to move a file. To do that, you just have to specify the source file and the destination directory or file.

mv source_file target_directory

This command will move the source_file and put it in the target_directory.

2. How to move multiple files

If you want to move multiple files at once, just provide all the files to the move command followed by the destination directory.

mv file1.txt file.2.txt file3.txt target_directory

You can also use glob to move multiple files matching a pattern.

For example in the above example, instead of providing all the files individually, you can also use the glob that matches all the files with the extension .txt and moves them to the target directory.

3. How to rename a file

One essential use of mv command is in renaming of files. If you use mv command and specify a file name in the destination, the source file will be renamed to the target_file.

mv source_file target_directory/target_file

In the above example, if the target_fille doesn’t exist in the target_directory, it will create the target_file.

However, if the target_file already exists, it will overwrite it without asking. Which means the content of the existing target file will be changed with the content of the source file.

I’ll show you how to deal with overwriting of files with mv command later in this tutorial.

You are not obliged to provide a target directory. If you don’t specify the target directory, the file will be renamed and kept in the same directory.

Keep in mind: By default, mv command overwrites if the target file already exists. This behavior can be changed with -n or -i option, explained later.

4. How to move a directory in Linux with mv command

You can use mv command to move directories as well. The command is the same as what we saw in moving files.

mv source_directory target_directory

In the above example, if the target_directory exists, the entire source_directory will be moved inside the target_directory. Which means that the source_directory will become a sub-directory of the target_directory.

Читайте также:  Linux systemctl list services

5. How to rename a directory

Renaming a directory is the same as moving a directory. The only difference is that the target directory must not already exist. Otherwise, the entire directory will be moved inside it as we saw in the previous directory.

mv source_directory path_to_non_existing_directory

6. How to deal with overwriting a file while moving

If you are moving a file and there is already a file with the same name, the contents of the existing file will be overwritten immediately.

This may not be ideal in all the situations. You have a few options to deal with the overwrite scenario.

To prevent overwriting existing files, you can use the -n option. This way, mv won’t overwrite existing files.

mv -n source_file target_directory

But maybe you want to overwrite some files. You can use the interactive option -i and it will ask you if you want to overwrite existing file(s).

mv -i source_file target_directory mv: overwrite 'target_directory/source_file'?

You can enter y for overwriting the existing file or n for not overwriting it.

There is also an option for making automatic backups. If you use -b option with the mv command, it will overwrite the existing files but before that, it will create a backup of the overwritten files.

mv -b file.txt target_dir/file.txt ls target_dir file.txt file.txt~

By default, the backup of the file ends with ~. You can change it by using the -S option and specifying the suffix:

mv -S .back -b file.txt target_dir/file.txt ls target_dir file.txt file.txt.back

You can also use the update option -u when dealing with overwriting. With the -u option, source files will only be moved to the new location if the source file is newer than the existing file or if it doesn’t exist in the target directory.

  • -i : Confirm before overwriting
  • -n : No overwriting
  • -b : Overwriting with backup
  • -u : Overwrite if the target file is old or doesn’t exist

7. How to forcefully move the files

If the target file is write protected, you’ll be asked to confirm before overwriting the target file.

mv file1.txt target mv: replace 'target/file1.txt', overriding mode 0444 (r--r--r--)? y

To avoid this prompt and overwrite the file straightaway, you can use the force option -f.

If you do not know what’s write protection, please read about file permissions in Linux.

You can further learn about mv command by browsing its man page. However, you are more likely to use only these mv commands examples I showed here. FYI, you may also use rename command for renaming multiple files at once.

I hope you like this article. If you have questions or suggestions, please feel free to ask in the comment section below.

Источник

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. copy syntax We can simply use the cp command along with the source and destination. copy file single 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
Читайте также:  Эмулятор wine для linux

copy single file prompt

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 content directory

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.

copy directory contents

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

copy multiple syntax

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

copy multiple to directory

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.

move syntax

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

move files dir

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

Читайте также:  Accessing mysql in linux

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

move directory

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 updated 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 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

rename syntax

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

rename file 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

rename folder

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.

Источник

Оцените статью
Adblock
detector