Signup/Sign In

How To Move Files In Linux Using The mv Command

Some actions are done so routinely; people take for granted precisely how easy they are. But then, you relocate to a new platform, and the same easy actions begin to need a small percentage of your brain’s capacity to execute. One such job is shifting files from one area to another. Sure, it’s most typically regarded as one of the most simple tasks to be done on a computer. When you switch to the Linux platform, though, you may find yourself wondering, “Now, how do I transfer files?”

If you’re acquainted with Linux, you know there are always numerous roads to the same result. Moving files is no exception. You may select for the strength of the command line or the ease of the GUI — either way, you will get those files relocated.

Let’s look precisely how you can transfer those files around.

Command-line moving

One of the challenges that many people new to Linux confront is the thought of having to utilize the command line. It might be pretty frightening at first. Although contemporary Linux interfaces may assist in guaranteeing you seldom have to use this “old school” tool, there is an excellent lot of power you would be losing if you disregarded it. The command for relocating files is an excellent instance of this.

The command to relocate files is mv. It’s fundamental and one of the first instructions you will learn on the platform. Instead of simply laying down the syntax and the regular switches for the command – and then letting you handle the rest – let’s go through how you can make use of this tool.

The mv command does one thing — it copies a file from one place to another. This may be slightly deceptive since mv is also used to rename files. How? Simple. Here’s an example. Say you have the file testfile at /home/jack/, and you want to rename it to testfile2 (while maintaining it in the same place) (while keeping it in the exact location). To achieve this, you would use the mv command like so:

mv /home/jack/testfile /home/jack/testfile2

Alternatively, if you’re already inside /home/jack:

mv testfile testfile2

The aforementioned instructions would relocate /home/jack/testfile to /home/jack/testfile2 - essentially renaming the file. But what if you merely wanted to relocate the file? Say you wish to keep your home directory (in this example /home/jack) free from stray files. You might transfer the testfile into /home/jack/Documents using the command:

mv /home/jack/test file /home/jack/Documents/

You have moved the file into a new place with the above command while maintaining the old file name.

What if you have a bunch of files you wish to move? Luckily, you don’t have to perform the mv command for every file. You may use wildcards to assist you out. Here’s an example:

You have a lot of .mp3 files in your ~/Downloads directory (~/ – is an easy way to represent your home directory – in our previous example, that would be /home/jack/) and you want them in ~/Music. You could swiftly relocate them with a single command, like so:

mv ~/Downloads/*.mp3 ~/Music/

That command would transfer every file that ended in .mp3 from the Downloads directory and place them into the Music directory.

Should you wish to relocate a file into the parent directory of the current working directory, there’s a simple approach to achieve it. Say you have the file testfile stored in ~/Downloads, and you want it in your home directory. If you are presently in the ~/Downloads directory, you may move it up one folder (to ~/) like so:

mv testfile ../

The “../” implies moving the folder up one level. If you’re buried deeper, say ~/Downloads/today/, you can still quickly relocate that file with:

mv testfile ../../

Just remember, each “../” indicates one level up.

As you can see, transferring files from the command line isn’t difficult at all.

About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.