How to move file in linux in terminal

Terminal Basics #8: Move Files and Directories (Cut-Paste Operation)

In the eighth chapter of the Terminal Basics series, learn about moving files and directories using the mv command in Linux.

Cut, copy and paste are part of everyday computing life. In the previous chapter, you learned about copying files and folders (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 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. 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 

Example of moving files in Linux using the mv command

Moving multiple files

mv file1 file2 fileN destination_directory
mv file_2 file_3 file_4 dir3

Example of moving multiple files in Linux

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'?

Example of moving interactively in Linux

You can press N to deny replacement and Y or Enter to replace the destination file.

Читайте также:  Установленные пакеты oracle linux

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
[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$ 

Using move command with update option

As you can see, the move command was executed at 10:41 and only the timestamp of file_2 has been changed.

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

Handling target is not a directory error in Linux

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: 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.

Moving folders in Linux command line

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 . You can move multiple directories the same way.

Rename files and directories

mv filename new_name_in_same_or_new_location

Rename files with mv command

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

Читайте также:  How to update linux ubuntu

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.

DuckDuckGo hidden features

Hidden Features! 25 Fun Things You Can Do With DuckDuckGo Search Engine

What is TTY in Linux

What is TTY in Linux?

s-tui is used for CPU utilization monitoring

Beautifully Monitor CPU Utilization in Linux Terminal With Stress Terminal UI

Access Grub in VM

How to Access the GRUB Menu in Virtual Machine

FOSS Weekly 23.28

FOSS Weekly #23.28: China’s Linux OS, Linux Exit Codes, Btrfs Origins and More

cp command in Linux

Using cp Command in Linux

Become a Better Linux User

With the FOSS Weekly Newsletter, you learn useful Linux tips, discover applications, explore new distros and stay updated with the latest from Linux world

Источник

How to move a file in Linux

Whether you’re new to moving files in Linux or experienced, you’ll learn something in this in-depth writeup.

Files in a folder

Moving files in Linux can seem relatively straightforward, but there are more options available than most realize. This article teaches beginners how to move files in the GUI and on the command line, but also explains what’s actually happening under the hood, and addresses command line options that many experience users have rarely explored.

Moving what?

Before delving into moving files, it’s worth taking a closer look at what actually happens when moving file system objects. When a file is created, it is assigned to an inode, which is a fixed point in a file system that’s used for data storage. You can find what inode maps to a file with the ls command:

$ ls --inode example.txt 7344977 example.txt 

When you move a file, you don’t actually move the data from one inode to another, you only assign the file object a new name or file path. In fact, a file retains its permissions when it’s moved, because moving a file doesn’t change or re-create it.

File and directory inodes never imply inheritance and are dictated by the filesystem itself. Inode assignment is sequential based on when the file was created and is entirely independent of how you organize your computer. A file «inside» a directory may have a lower inode number than its parent directory, or a higher one. For example:

$ mkdir foo $ mv example.txt foo $ ls --inode 7476865 foo $ ls --inode foo 7344977 example.txt 

When moving a file from one hard drive to another, however, the inode is very likely to change. This happens because the new data has to be written onto a new filesystem. For this reason, in Linux the act of moving and renaming files is literally the same action. Whether you move a file to another directory or to the same directory with a new name, both actions are performed by the same underlying program.

Читайте также:  Помощь при установке линукс

This article focuses on moving files from one directory to another.

Moving with a mouse

The GUI is a friendly and, to most people, familiar layer of abstraction on top of a complex collection of binary data. It’s also the first and most intuitive way to move files on Linux. If you’re used to the desktop experience, in a generic sense, then you probably already know how to move files around your hard drive. In the GNOME desktop, for instance, the default action when dragging and dropping a file from one window to another is to move the file rather than to copy it, so it’s probably one of the most intuitive actions on the desktop:

Moving a file in GNOME.

The Dolphin file manager in the KDE Plasma desktop defaults to prompting the user for an action. Holding the Shift key while dragging a file forces a move action:

Источник

Move files in the Linux terminal

To move a file on a computer with a graphical interface, you open the folder where the file is currently located, and then open another window to the folder you want to move the file into. Finally, you drag and drop the file from one to the other.

To move a file in a terminal, you use the mv command to move a file from one location to another.

$ mv example.txt ~/Documents $ ls ~/Documents example.txt

In this example, you’ve moved example.txt from its current folder into the Documents folder.

As long as you know where you want to take a file from and where you want to move it to, you can send files from any location to any location, no matter where you are. This can be a serious time saver compared to navigating through all the folders on your computer in a series of windows just to locate a file, and then opening a new window to where you want that file to go, and then dragging that file.

The mv command by default does exactly as it’s told: it moves a file from one location to another. Should a file with the same name already exist in the destination location, it gets overwritten. To prevent a file from being overwritten without warning, use the —interactive (or -i for short) option:

$ mv -i example.txt ~/Documents mv: overwrite '/home/tux/Documents/example.txt'? 

Move around your computer

Use the Linux terminal to navigate throughout your computer

Learn to navigate from directory to directory in the Linux terminal.

Open a directory

How to open and close directories in the Linux terminal

Learn how to use the cd command to change directories with this Linux tutorial.

List files on your computer

Use the Linux terminal to see what files are on your computer

Learn how to use the ls command to list files in the terminal with this Linux tutorial.

Источник

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