How to move a file in Linux (2024)

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.txt7344977 example.txt

When you movea 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 movinga 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 --inode7476865 foo$ ls --inode foo7344977 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:

How to move a file in Linux (1)

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:

Moving on the command line

The Linux Terminal

The shell command intended for moving files on Linux, BSD, Illumos, Solaris, and MacOS is mv. A simple command with a predictable syntax, mv <source> <destination> moves a source file to the specified destination, each defined by either an absolute or relative file path. As mentioned before, mv is such a common command for POSIX users that many of its additional modifiers are generally unknown, so this article brings a few useful modifiers to your attention whether you are new or experienced.

Not all mv commands were written by the same people, though, so you may have GNU mv, BSD mv, or Sun mv, depending on your operating system. Command options differ from implementation to implementation (BSD mv has no long options at all) so refer to your mv man page to see what’s supported, or install your preferred version instead (that’s the luxury of open source).

Moving a file

To move a file from one folder to another with mv, remember the syntax mv <source> <destination>. For instance, to move the file example.txt into your Documents directory:

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

Just like when you move a file by dragging and dropping it onto a folder icon, this command doesn’t replace Documents with example.txt. Instead, mv detects that Documents is a folder, and places the example.txt file into it.

You can also, conveniently, rename the file as you move it:

$ touch example.txt$ mv example.txt ~/Documents/foo.txt$ ls ~/Documentsfoo.txt

That’s important because it enables you to rename a file even when you don’t want to move it to another location, like so:

$ touch example.txt$ mv example.txt foo2.txt$ lsfoo2.txt

Moving a directory

The mv command doesn’t differentiate a file from a directory the way cp does. You can move a directory or a file with the same syntax:

$ touch file.txt$ mkdir foo_directory$ mv file.txt foo_directory$ mv foo_directory ~/Documents

Moving a file safely

If you copy a file to a directory where a file of the same name already exists, the mv command replaces the destination file with the one you are moving, by default. This behavior is called clobbering, and sometimes it’s exactly what you intend. Other times, it is not.

Some distributions alias (or you might write your own) mv to mv --interactive, which prompts you for confirmation. Some do not. Either way, you can use the --interactive or -i option to ensure that mv asks for confirmation in the event that two files of the same name are in conflict:

$ mv --interactive example.txt ~/Documentsmv: overwrite '~/Documents/example.txt'? 

If you do not want to manually intervene, use --no-clobber or -n instead. This flag silently rejects the move action in the event of conflict. In this example, a file named example.txt already exists in ~/Documents, so it doesn't get moved from the current directory as instructed:

$ mv --no-clobber example.txt ~/Documents$ lsexample.txt

Moving with backups

If you’re using GNU mv, there are backup options offering another means of safe moving. To create a backup of any conflicting destination file, use the -b option:

$ mv -b example.txt ~/Documents$ ls ~/Documentsexample.txt example.txt~

This flag ensures that mv completes the move action, but also protects any pre-existing file in the destination location.

Another GNU backup option is --backup, which takes an argument defining how the backup file is named:

  • existing: If numbered backups already exist in the destination, then a numbered backup is created. Otherwise, the simple scheme is used.
  • none: Does not create a backup even if --backup is set. This option is useful to override a mv alias that sets the backup option.
  • numbered: Appends the destination file with a number.
  • simple: Appends the destination file with a ~, which can conveniently be hidden from your daily view with the --ignore-backups option for ls.

For example:

$ mv --backup=numbered example.txt ~/Documents$ ls ~/Documents-rw-rw-r--. 1 seth users 128 Aug 1 17:23 example.txt-rw-rw-r--. 1 seth users 128 Aug 1 17:20 example.txt.~1~

A default backup scheme can be set with the environment variable VERSION_CONTROL. You can set environment variables in your ~/.bashrc file or dynamically before your command:

$ VERSION_CONTROL=numbered mv --backup example.txt ~/Documents$ ls ~/Documents-rw-rw-r--. 1 seth users 128 Aug 1 17:23 example.txt-rw-rw-r--. 1 seth users 128 Aug 1 17:20 example.txt.~1~-rw-rw-r--. 1 seth users 128 Aug 1 17:22 example.txt.~2~

The --backup option still respects the --interactive or -i option, so it still prompts you to overwrite the destination file, even though it creates a backup before doing so:

$ mv --backup=numbered example.txt ~/Documentsmv: overwrite '~/Documents/example.txt'? y$ ls ~/Documents-rw-rw-r--. 1 seth users 128 Aug 1 17:24 example.txt-rw-rw-r--. 1 seth users 128 Aug 1 17:20 example.txt.~1~-rw-rw-r--. 1 seth users 128 Aug 1 17:22 example.txt.~2~-rw-rw-r--. 1 seth users 128 Aug 1 17:23 example.txt.~3~

You can override -i with the --force or -f option.

$ mv --backup=numbered --force example.txt ~/Documents$ ls ~/Documents-rw-rw-r--. 1 seth users 128 Aug 1 17:26 example.txt-rw-rw-r--. 1 seth users 128 Aug 1 17:20 example.txt.~1~-rw-rw-r--. 1 seth users 128 Aug 1 17:22 example.txt.~2~-rw-rw-r--. 1 seth users 128 Aug 1 17:24 example.txt.~3~-rw-rw-r--. 1 seth users 128 Aug 1 17:25 example.txt.~4~

The --backup option is not available in BSD mv.

Moving many files at once

When moving multiple files, mv treats the final directory named as the destination:

$ mv foo bar baz ~/Documents$ ls ~/Documentsfoo bar baz

If the final item is not a directory, mv returns an error:

$ mv foo bar bazmv: target 'baz' is not a directory

The syntax of GNU mv is fairly flexible. If you are unable to provide the mv command with the destination as the final argument, use the --target-directory or -t option:

$ mv --target-directory=~/Documents foo bar baz$ ls ~/Documentsfoo bar baz

This is especially useful when constructing mv commands from the output of some other command, such as the find command, xargs, or GNU Parallel.

Moving based on mtime

With GNU mv, you can define a move action based on whether the file being moved is newer than the destination file it would replace. This option is possible with the --update or -u option, and is not available in BSD mv:

$ ls -l ~/Documents-rw-rw-r--. 1 seth users 128 Aug 1 17:32 example.txt$ ls -l -rw-rw-r--. 1 seth users 128 Aug 1 17:42 example.txt$ mv --update example.txt ~/Documents$ ls -l ~/Documents-rw-rw-r--. 1 seth users 128 Aug 1 17:42 example.txt$ ls -l

This result is exclusively based on the files’ modification time, not on a diff of the two files, so use it with care. It’s easy to fool mv with a mere touch command:

$ cat example.txtone$ cat ~/Documents/example.txtonetwo$ touch example.txt$ mv --update example.txt ~/Documents$ cat ~/Documents/example.txtone

Obviously, this isn’t the most intelligent update function available, but it offers basic protection against overwriting recent data.

Moving

There are more ways to move data than just the mv command, but as the default program for the job, mv is a good universal option. Now that you know what options you have available, you can use mv smarter than ever before.

How to move a file in Linux (3)This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.

How to move a file in Linux (2024)

FAQs

How to move a file in Linux command line? ›

Use the mv command to move files and directories from one directory to another or to rename a file or directory. If you move a file or directory to a new directory without specifying a new name, it retains its original name. Attention: The mv command can overwrite many existing files unless you specify the -i flag.

How do I move a file system in Linux? ›

How to Move a File in Linux Using mv Command. Enter your source file name in place of [source_file_name(s)] and your destination path in place of [Destination_path]. For Example: If we have a file “name = geeksforgeeks” and want to move it to location “name = /home/jayeshkumar/jkj”.

How do I move selected files in Linux? ›

The mv command is a powerful tool that can be used to move files and directories in Linux. Whether you need to rename a file, move a file to a new location, or move multiple files simultaneously, the mv command has options and arguments to make the process efficient and effective.

How do I move files in the current directory in Linux? ›

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.

How do I move a file? ›

Use the Move or Copy option

Alternatively, right-click the item and depending on what you want to do, select Move or Copy to folder. Select a folder from the drop-down list. If your folder isn't listed, select Move to a different folder (or Copy to a different folder).

What is the Find command to move the file in Linux? ›

It can be somewhat daunting at first. Although modern Linux interfaces can help to ensure you rarely have to use this “old school” tool, there is a great deal of power you would be missing if you ignored it altogether. The command for moving files is a perfect illustration of this. The command to move files is mv .

How do I move files from local to Linux? ›

Transfer Files via SCP. One of the most common secure ways to transfer files from Windows to Linux is to use the scp (Secure Copy) client. To do that, we need an scp client for our Windows system.

How do I move an executable file in Linux? ›

  1. It *really* depends on the application.
  2. Some applications will let you do exactly that - just copy the .exe file - and they'll run. Just copy it to a thumb drive or over the network like any other file. ...
  3. Other applications will have a host of other files that that .exe file needs, but will still let you just move th.
Dec 27, 2023

How do I move a swap file in Linux? ›

Here's how you can do it:
  1. Open Terminal (if it's not already open)
  2. Create a swap file of equal (or greater) size to your swap partition: sudo fallocate -l 16G /swapfile.
  3. Set the file permissions: sudo chmod 600 /swapfile.
  4. Mark the file as a swap location: sudo mkswap /swapfile.
  5. Enable the swap file: sudo swapon /swapfile.
Feb 21, 2021

How do I move one folder in Linux? ›

Select the directory you want to move and press Ctrl+X. Alternatively, right-click the directory and select Cut from the drop-down menu. 2. Navigate to the destination and press Ctrl+V or right-click the empty space and select Paste from the drop-down menu to move the directory.

What is the RM command in Linux? ›

rm stands for remove here. rm command is used to remove objects such as files, directories, symbolic links and so on from the file system like UNIX.

How do you move instead of copy in Linux? ›

3 Commands to Use in the Linux Command Line:
  1. mv: Moving (and Renaming) Files. The mv command lets you move a file from one directory location to another. ...
  2. cp: Copying Files. ...
  3. rm: Deleting Files.
Mar 25, 2023

How to move a file in Linux terminal? ›

To move a file, type mv followed by the file you want to move and its destination as shown in the figure below. mv can also be used to rename a file, to do this, use mv followed by the current filename and its new name as shown below.

How to move directory in terminal? ›

cd or change directory

The cd command allows you to move between directories. The cd command takes an argument, usually the name of the folder you want to move to, so the full command is cd your-directory .

How do I move a working directory in Linux? ›

The cd (change directory) command moves you into a different directory. To move out of that directory, use cd along with the path to some other location, or use double dots to backtrack, or return home to navigate from there. Navigating a Linux computer is like navigating the internet.

How to transfer file command line Linux? ›

scp (secure copy) command in Linux system is used to copy file(s) between servers in a secure way. The SCP command or secure copy allows the secure transferring of files between the local host and the remote host or between two remote hosts.

How do I move files in command prompt? ›

For this, write "Command Prompt" in the text box and run it as an administrator. Now, write the prompt using the command following the "move [filename] [destination]" format. Finally, hit the "Enter" key to move your files from one drive to another within the same PC.

Top Articles
How To Move a Shipping Container
Capital One Financial Net Income 2010-2024 | COF
Menards Thermal Fuse
Bank Of America Financial Center Irvington Photos
My E Chart Elliot
Mr Tire Prince Frederick Md 20678
Doublelist Paducah Ky
Songkick Detroit
Ukraine-Russia war: Latest updates
Nier Automata Chapter Select Unlock
What is Cyber Big Game Hunting? - CrowdStrike
The Superhuman Guide to Twitter Advanced Search: 23 Hidden Ways to Use Advanced Search for Marketing and Sales
Theresa Alone Gofundme
How do I get into solitude sewers Restoring Order? - Gamers Wiki
Video shows two planes collide while taxiing at airport | CNN
Pretend Newlyweds Nikubou Maranoshin
Candy Land Santa Ana
MLB power rankings: Red-hot Chicago Cubs power into September, NL wild-card race
Raz-Plus Literacy Essentials for PreK-6
Isaidup
Craigslist Houses For Rent In Milan Tennessee
CVS Health’s MinuteClinic Introduces New Virtual Care Offering
208000 Yen To Usd
Mynahealthcare Login
Safeway Aciu
Swimgs Yuzzle Wuzzle Yups Wits Sadie Plant Tune 3 Tabs Winnie The Pooh Halloween Bob The Builder Christmas Autumns Cow Dog Pig Tim Cook’s Birthday Buff Work It Out Wombats Pineview Playtime Chronicles Day Of The Dead The Alpha Baa Baa Twinkle
Life Insurance Policies | New York Life
A Grade Ahead Reviews the Book vs. The Movie: Cloudy with a Chance of Meatballs - A Grade Ahead Blog
Marie Peppers Chronic Care Management
D3 Boards
Wsbtv Fish And Game Report
Doordash Promo Code Generator
60 X 60 Christmas Tablecloths
Engr 2300 Osu
Nid Lcms
Mitchell Kronish Obituary
Yakini Q Sj Photos
John M. Oakey & Son Funeral Home And Crematory Obituaries
Brown launches digital hub to expand community, career exploration for students, alumni
What is a lifetime maximum benefit? | healthinsurance.org
Movie Hax
2294141287
Kate Spade Outlet Altoona
Arginina - co to jest, właściwości, zastosowanie oraz przeciwwskazania
Ronnie Mcnu*t Uncensored
Clock Batteries Perhaps Crossword Clue
Diamond Desires Nyc
Competitive Comparison
Denys Davydov - Wikitia
Ark Silica Pearls Gfi
Latest Posts
Article information

Author: Kieth Sipes

Last Updated:

Views: 5834

Rating: 4.7 / 5 (67 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Kieth Sipes

Birthday: 2001-04-14

Address: Suite 492 62479 Champlin Loop, South Catrice, MS 57271

Phone: +9663362133320

Job: District Sales Analyst

Hobby: Digital arts, Dance, Ghost hunting, Worldbuilding, Kayaking, Table tennis, 3D printing

Introduction: My name is Kieth Sipes, I am a zany, rich, courageous, powerful, faithful, jolly, excited person who loves writing and wants to share my knowledge and understanding with you.