- How to rename and move files and directories on Linux
- Overview
- Rename files on Linux
- Moving a file on Linux
- Moving and Renaming files on Linux
- Moving Multiple files on Linux
- Moving Directories on Linux
- Verbose Output Flag
- Do Not Overwrite Existing Files
- Do Not Prompt to Confirm Overwrites
- Moving files and directories in linux
- How to Copy / Move Files and Directories in Linux with “cp” and “mv” commands
- Copy Files and Directories
- Example 1
- Example 2
- Example 3
- Moving Files and Directories
- How to Move Files Using Linux Commands or File Managers
- Command line moving
How to rename and move files and directories on Linux
Overview
In this tutorial, you will learn how to use the mv command to move and renames files and directories on Linux.
Files and directories on Linux as very similar, from a filesystem point of view. This means the operations done on one can be also done on the other, with very few exceptions.
As such, you will notice that commands used to perform actions on files are identical with directories.
Rename files on Linux
To rename a file in Linux you use the mv command. The command accepts two or more arguments. For renaming files, only two arguments are needed, which are the source file and the target file.
The mv command will take the source file specified and rename it to the target file.
To rename a file named student1 to student10, for example, you would run the following command.
Provided the file target is the same directory, all file attributes will remain, including permissions.
Moving a file on Linux
To move a file to another location we use the same method as renaming a file, except the file path should be different.
For example, to move a file from /home/student1/lab-work.log to /var/labs/student1/lab-work.log , you would run the following command.
Moving and Renaming files on Linux
A file can be renamed during a move process using the mv command. You simply give the target path a different name. When mv moves the file, it will be given a new name.
For example, to move a file named student1.txt to /var/students and rename it to class1-student1.txt , you would run the following command.
Moving Multiple files on Linux
The mv command accepts multiple source files, which means we can move two or more files at the same time. When executing the mv command, each file listed will be considered a source with the last path being the exception. The last path will be treated as the target.
For example, to move student1.txt and student2.txt to /var/students , you would run the following command.
Moving Directories on Linux
Moving directories work the same as moving files. We specify the source directory and give a target directory.
For example, to move a directory path /tmp/logs to
/data/logs you would run the following command.
Moving Multiple Directories on Linux
As with files, multiple directories can be moved to a new location. We simply specially all of the directories to be moved, and then give a target directory for them to be moved to.
Verbose Output Flag
The mv command will perform its operations silently. No output will be printed to the screen while files or directories are being moved or renamed.
To instruct the mv command to print out a log of actions being taken, you can use the -v flag. This flag enabled verbosity, which is helpful for auditing.
Do Not Overwrite Existing Files
To force the mv command to not overwrite existing files when moving or renaming a file, use the -n flag.
In the example below, if the student2.txt file already exists, then the mv command will not rename the file and it will exit with an error.
Do Not Prompt to Confirm Overwrites
If you want to forcefully move files or directories and overwrite paths that already exist, you can use the -f flag. This is effective for overwriting old, stale files or directories with new ones with the same name.
Источник
Moving files and directories in linux
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.
Don’t be too «forceful» |
---|
Be careful! |
---|