HomeLinuxFind out how to Transfer Information and Folders in Linux Command Line

Find out how to Transfer Information and Folders in Linux Command Line


Reduce, copy and paste are a part of on a regular basis computing life.

Within the earlier chapter, you discovered about copying information and folders (directories) within the terminal.

On this a part of the Terminal Fundamentals sequence, you may be taught concerning the cut-paste operation (shifting) within the Linux terminal.

Transferring or cut-paste?

Alright! Reduce-paste is just not the proper technical time period right here. It’s referred to as shifting information (and folders).

Since you’re new to the command line, it’s possible you’ll discover the time period ‘shifting’ complicated.

While you copy a file to a different location utilizing the cd command, the supply file stays in the identical location.

While you transfer a file to a different location utilizing the mv command, the supply file not stays within the origin location.

This is identical cut-paste operation (Ctrl+X and Ctrl+V) you do in a graphical file explorer.

📋

Mainly, shifting information within the command line could be thought identical as cut-paste in a graphical atmosphere.

Transferring information

Linux has a devoted mv command (brief for transfer) for shifting information and directories to different places.

And utilizing the mv command is sort of easy:

mv source_file destination_directory

The function of path involves play right here as properly. You should utilize both the absolute or relative path. Whichever fits your want.

Let’s examine this with an instance. You must apply together with it by replicating the instance situations in your system.

That is the listing construction within the instance:

[email protected]:~/moving_files$ tree
.
├── dir1
│   ├── file_2
│   └── file_3
├── dir2
│   └── passwd
├── dir3
├── file_1
├── file_2
├── file_3
├── file_4
├── passwd
└── providers

3 directories, 9 information

Now, for example I wish to transfer the file_1 to dir3.

mv file_1 dir3
Example of moving files in Linux using the mv command

Transferring a number of information

You may transfer a number of information to a different location in the identical mv command:

mv file1 file2 fileN destination_directory

Let’s proceed our instance state of affairs to maneuver a number of information.

mv file_2 file_3 file_4 dir3
Example of moving multiple files in Linux

🖥️

Transfer the information again to the present listing from dir3. We want them within the subsequent examples.

Transferring information with warning

If the vacation spot already has information with the identical identify, the vacation spot information will likely be changed instantly. At occasions, you will not need that.

Just like the cp command, the mv command additionally has an interactive mode with choice -i.

And the aim is identical. Ask for affirmation earlier than changing the information on the vacation spot.

[email protected]:~/moving_files$ mv -i file_3 dir1
mv: overwrite 'dir1/file_3'?

You may press N to disclaim substitute and Y or Enter to exchange the vacation spot file.

Example of moving interactively in Linux

Transfer however solely replace

The mv command comes with some particular choices. One in every of them is the replace choice -u.

With this, the vacation spot file will solely get replaced if the file being moved is newer than it.

mv -u file_name destination_directory

Here is an instance. 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

Within the vacation spot listing dir1, file_2 was final modified at 10:37 and file_3 was modified at 10:39.

[email protected]:~/moving_files$ ls -l dir1
complete 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 different phrases, within the vacation spot listing, the file_2 is older and file_3 is newer than those being moved.

It additionally signifies that file_3 will not me moved whereas as file_2 will likely be up to date. You may confirm it with the timestamps of the information within the vacation spot listing after operating the mv command.

[email protected]:~/moving_files$ mv -u file_2 file_3 dir1
[email protected]:~/moving_files$ ls -l dir1
complete 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’ll be able to see, the transfer command was executed at 10:41 and solely the timestamp of file_2 has been modified.

Using move command with update option

💡

You may also use the backup choice -b. If the vacation spot file is being changed, it’ll robotically create a backup with the filename~ sample.

Troubleshoot: Goal is just not a listing

If you’re shifting a number of information, the final argument should be a listing. In any other case, you may encounter this error:

goal is just not a listing

Right here, I create a file which is known as dir. The identify feels like a listing, however it’s a file. And when I attempt to transfer a number of information to it, the plain error is there:

Handling target is not a directory error in Linux

However what in the event you transfer a single file to a different file? In that case, the goal file is changed by the supply file’s content material whereas the supply file is renamed because the goal file. Extra on this in later sections.

Transferring directories

To date, you’ve got seen every little thing about shifting information. How about shifting directories?

The cp and rm instructions used recusrive choice -r to repeat and delete folders respectively.

Nevertheless, there is no such thing as a such requirement for the mv command. You should utilize the mv command as it’s for shifting directories.

mv dir target_directory

Here is an instance the place I transfer the dir2 listing to dir3. And as you’ll be able to see, dir2 together with its content material is moved to dir3.

Moving folders in Linux command line

You may transfer a number of directories the identical manner.

Rename information and directories

If you wish to rename a file or listing, you should use the identical mv command.

mv filename new_name_in_same_or_new_location

To illustrate you wish to rename a file in the identical location. Here is an instance the place I rename file_1 to file_one in the identical listing.

Rename files with mv command

You may also transfer and rename the information. You simply have to offer the listing path and the file identify of the vacation spot. Right here, I rename providers file to my_services whereas shifting it to dir3.

[email protected]:~/moving_files$ ls
dir  dir1  dir3  file_2  file_3  file_one  passwd  providers
[email protected]:~/moving_files$ mv providers dir3/my_services
[email protected]:~/moving_files$ ls dir3
dir2  my_services

📋

You can’t rename a number of information instantly with mv command. You must mix it with different instructions like discover and so forth. 

Check your data

Time to apply what you simply discovered.

Create a brand new folder to apply the train. In right here, create a listing construction like this:

.
├── dir1
├── dir2
│   ├── dir21
│   ├── dir22
│   └── dir23
└── dir3

Copy the file /and so forth/passwd to the present listing. Now rename it secrets and techniques.

Make three new information named file_1, file_2 and file_3. Transfer all of the information to dir22.

Now transfer the dir22 listing to dir3.

Delete all contents of dir2 now.

Within the penultimate chapter of the Terminal Fundamentals sequence, you may find out about enhancing information within the terminal. Keep tuned.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments