mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-29 21:41:00 +08:00
285 lines
8.9 KiB
Markdown
285 lines
8.9 KiB
Markdown
[#]: subject: "Linux Terminal Basics #8: Move Files and Directories (Cut-Paste Operation)"
|
||
[#]: via: "https://itsfoss.com/move-files-linux/"
|
||
[#]: author: "Abhishek Prakash https://itsfoss.com/author/abhishek/"
|
||
[#]: collector: "lkxed"
|
||
[#]: translator: " "
|
||
[#]: reviewer: " "
|
||
[#]: publisher: " "
|
||
[#]: url: " "
|
||
|
||
Linux Terminal Basics #8: Move Files and Directories (Cut-Paste Operation)
|
||
======
|
||
|
||
![][1]
|
||
|
||
Cut, copy and paste are part of everyday computing life.
|
||
|
||
In the previous chapter, you learned about [copying files and folders][2] (directories) in the terminal.
|
||
|
||
In this part of the Terminal Basics series, you'll learn about the cut-paste operation (moving) in the Linux terminal.
|
||
|
||
### Moving or cut-paste?
|
||
|
||
Alright! Cut-paste is not the correct technical term here. It is called moving files (and folders).
|
||
|
||
Since you are new to the command line, you may find the term 'moving' confusing.
|
||
|
||
When you copy a file to another location using the **cp** command, the source file remains in the same location.
|
||
|
||
When you move a file to another location **using the mv command**, the source file no longer remains in the origin location.
|
||
|
||
This is the same cut-paste operation (Ctrl+X and Ctrl+V) you do in a graphical file explorer.
|
||
|
||
> 📋 Basically, moving files in the command line can be thought same as cut-paste in a graphical environment.
|
||
|
||
### Moving files
|
||
|
||
Linux has a dedicated mv command (short for move) for moving files and directories to other locations.
|
||
|
||
And [using the mv command][3] is quite simple:
|
||
|
||
```
|
||
mv source_file destination_directory
|
||
```
|
||
|
||
The role of path comes to play here as well. You can use either the [absolute or relative path][4]. Whichever suits your need.
|
||
|
||
Let's see this with an example. **You should practice along with it by replicating the example scenarios on your system**.
|
||
|
||
This is the directory structure in the example:
|
||
|
||
```
|
||
[email protected]:~/moving_files$ tree
|
||
.
|
||
├── dir1
|
||
│ ├── file_2
|
||
│ └── file_3
|
||
├── dir2
|
||
│ └── passwd
|
||
├── dir3
|
||
├── file_1
|
||
├── file_2
|
||
├── file_3
|
||
├── file_4
|
||
├── passwd
|
||
└── services
|
||
|
||
3 directories, 9 files
|
||
```
|
||
|
||
Now, let's say I want to move the `file_1` to `dir3`.
|
||
|
||
```
|
||
mv file_1 dir3
|
||
```
|
||
|
||
![Example of moving files in Linux using the mv command][5]
|
||
|
||
#### Moving multiple files
|
||
|
||
You can move multiple files to another location in the same mv command:
|
||
|
||
```
|
||
mv file1 file2 fileN destination_directory
|
||
```
|
||
|
||
Let's continue our example scenario to move multiple files.
|
||
|
||
```
|
||
mv file_2 file_3 file_4 dir3
|
||
```
|
||
|
||
![Example of moving multiple files in Linux][6]
|
||
|
||
> 🖥️ Move the files back to the current directory from
|
||
|
||
```
|
||
dir3
|
||
```
|
||
|
||
. We need them in the next examples.
|
||
|
||
#### Moving files with caution
|
||
|
||
If the destination already has files with the same name, the destination files will be replaced immediately. At times, you won't want that.
|
||
|
||
Like the cp command, the mv command also has an interactive mode with option `-i`.
|
||
|
||
And the purpose is the same. Ask for confirmation before replacing the files at the destination.
|
||
|
||
```
|
||
[email protected]:~/moving_files$ mv -i file_3 dir1
|
||
mv: overwrite 'dir1/file_3'?
|
||
```
|
||
|
||
You can press N to deny replacement and Y or Enter to replace the destination file.
|
||
|
||
![Example of moving interactively in Linux][7]
|
||
|
||
#### Move but only update
|
||
|
||
The mv command comes with some special options. One of them is the update option `-u`.
|
||
|
||
With this, the destination file will only be replaced if the file being moved is newer than it.
|
||
|
||
```
|
||
mv -u file_name destination_directory
|
||
```
|
||
|
||
Here's an example. file_2 was modified at 10:39 and file_3 was modified at 10:06.
|
||
|
||
```
|
||
[email protected]:~/moving_files$ ls -l file_2 file_3
|
||
-rw-rw-r-- 1 abhishek abhishek 0 Apr 4 10:39 file_2
|
||
-rw-rw-r-- 1 abhishek abhishek 0 Apr 4 10:06 file_3
|
||
```
|
||
|
||
In the destination directory dir1, file_2 was last modified at 10:37 and file_3 was modified at 10:39.
|
||
|
||
```
|
||
[email protected]:~/moving_files$ ls -l dir1
|
||
total 0
|
||
-rw-rw-r-- 1 abhishek abhishek 0 Apr 4 10:37 file_2
|
||
-rw-rw-r-- 1 abhishek abhishek 0 Apr 4 10:39 file_3
|
||
```
|
||
|
||
In other words, in the destination directory, the file_2 is older and file_3 is newer than the ones being moved.
|
||
|
||
It also means that file_3 won't me moved while as file_2 will be updated. You can verify it with the timestamps of the files in the destination directory after running the mv command.
|
||
|
||
```
|
||
[email protected]:~/moving_files$ mv -u file_2 file_3 dir1
|
||
[email protected]:~/moving_files$ ls -l dir1
|
||
total 0
|
||
-rw-rw-r-- 1 abhishek abhishek 0 Apr 4 10:39 file_2
|
||
-rw-rw-r-- 1 abhishek abhishek 0 Apr 4 10:39 file_3
|
||
[email protected]:~/moving_files$ date
|
||
Tue Apr 4 10:41:16 AM IST 2023
|
||
[email protected]:~/moving_files$
|
||
```
|
||
|
||
As you can see, the move command was executed at 10:41 and only the timestamp of file_2 has been changed.
|
||
|
||
![Using move command with update option][8]
|
||
|
||
> 💡 You can also use the backup option
|
||
|
||
```
|
||
-b
|
||
```
|
||
|
||
. If the destination file is being replaced, it will automatically create a backup with the
|
||
|
||
```
|
||
filename~
|
||
```
|
||
|
||
pattern.
|
||
|
||
#### Troubleshoot: Target is not a directory
|
||
|
||
If you are moving multiple files, the last argument must be a directory. Otherwise, you'll encounter this error:
|
||
|
||
```
|
||
target is not a directory
|
||
```
|
||
|
||
Here, I create a file which is named `dir`. The name sounds like a directory, but it is a file. And when I try to move multiple files to it, the obvious error is there:
|
||
|
||
![Handling target is not a directory error in Linux][9]
|
||
|
||
But what if you move a single file to another file? In that case, the target file is replaced by the source file's content while the source file is renamed as the target file. More on this in later sections.
|
||
|
||
### Moving directories
|
||
|
||
So far, you have seen everything about moving files. How about moving directories?
|
||
|
||
The cp and rm commands used recusrive option -r to copy and delete folders respectively.
|
||
|
||
However, there is no such requirement for the mv command. You can use the mv command as it is for moving directories.
|
||
|
||
```
|
||
mv dir target_directory
|
||
```
|
||
|
||
Here's an example where I move the `dir2` directory to `dir3`. And as you can see, `dir2` along with its content is moved to `dir3`.
|
||
|
||
![Moving folders in Linux command line][10]
|
||
|
||
You can move multiple directories the same way.
|
||
|
||
### Rename files and directories
|
||
|
||
If you want to rename a file or directory, you can use the same mv command.
|
||
|
||
```
|
||
mv filename new_name_in_same_or_new_location
|
||
```
|
||
|
||
Let's say you want to rename a file in the same location. Here's an example where I rename `file_1` to `file_one` in the same directory.
|
||
|
||
![Rename files with mv command][11]
|
||
|
||
You can also move and rename the files. You just have to provide the directory path and the file name of the destination. Here, I rename `services` file to `my_services` while moving it to `dir3`.
|
||
|
||
```
|
||
[email protected]:~/moving_files$ ls
|
||
dir dir1 dir3 file_2 file_3 file_one passwd services
|
||
[email protected]:~/moving_files$ mv services dir3/my_services
|
||
[email protected]:~/moving_files$ ls dir3
|
||
dir2 my_services
|
||
```
|
||
|
||
> 📋 You cannot rename multiple files directly with mv command. You have to combine it with other commands like find etc.
|
||
|
||
### Test your knowledge
|
||
|
||
Time to practice what you just learned.
|
||
|
||
Create a new folder to practice the exercise. In here, create a directory structure like this:
|
||
|
||
```
|
||
.
|
||
├── dir1
|
||
├── dir2
|
||
│ ├── dir21
|
||
│ ├── dir22
|
||
│ └── dir23
|
||
└── dir3
|
||
```
|
||
|
||
Copy the file /etc/passwd to the current directory. Now rename it `secrets`.
|
||
|
||
Make three new files named `file_1`, `file_2` and `file_3`. Move all the files to `dir22`.
|
||
|
||
Now move the `dir22` directory to `dir3`.
|
||
|
||
Delete all contents of `dir2` now.
|
||
|
||
In the penultimate chapter of the Terminal Basics series, you'll learn about editing files in the terminal. Stay tuned.
|
||
|
||
--------------------------------------------------------------------------------
|
||
|
||
via: https://itsfoss.com/move-files-linux/
|
||
|
||
作者:[Abhishek Prakash][a]
|
||
选题:[lkxed][b]
|
||
译者:[译者ID](https://github.com/译者ID)
|
||
校对:[校对者ID](https://github.com/校对者ID)
|
||
|
||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||
|
||
[a]: https://itsfoss.com/author/abhishek/
|
||
[b]: https://github.com/lkxed/
|
||
[1]: https://itsfoss.com/content/images/2023/03/linux-mega-packt.webp
|
||
[2]: https://itsfoss.com/copy-files-directory-linux/
|
||
[3]: https://linuxhandbook.com/mv-command/?ref=itsfoss.com
|
||
[4]: https://linuxhandbook.com/absolute-vs-relative-path/?ref=itsfoss.com
|
||
[5]: https://itsfoss.com/content/images/2023/04/moving-files-linux.png
|
||
[6]: https://itsfoss.com/content/images/2023/04/moving_multiple_files_linux.png
|
||
[7]: https://itsfoss.com/content/images/2023/04/move-interactively-linux.png
|
||
[8]: https://itsfoss.com/content/images/2023/04/move-command-update-option.png
|
||
[9]: https://itsfoss.com/content/images/2023/04/target-is-not-a-directory-error-linux.png
|
||
[10]: https://itsfoss.com/content/images/2023/04/moving-directories.png
|
||
[11]: https://itsfoss.com/content/images/2023/04/rename-file-with-mv-command.png
|