How to Move Files in Linux mv Command

If you are wanting to know how to move files in Linux, you will need to use the Linux mv command. There are a few different options you can use with the Linux mv command, but I normally don’t use them. I do use the Linux mv command along with other commands to accomplish some tasks though. If you wanted to get information on the other options for the Linux mv command, remember you can always run man mv in a bash shell prompt.
The usage of the Linux mv command to move files around on your file system is much like the Linux cp command, but the source will no longer exist. To use the mv command in Linux shell, just run mv [source] [destination].
So for example I am making a new account on another Linux computer. I want to move my home directory to the new Linux computer. So first I setup nfs and mount my new Linux computer’s /home directory on my old Linux computer. Now I will run mv /home/max /mnt/home/ so all of my files move to my new Linux computer. This is just an example as I would most likely use rsync Linux command to do this. Which I will go over in another Linux tutorial.
I have also seen a few Linux beginners having trouble moving files in Linux that have special characters in them. So to move files with special characters in Linux you will simple escape the characters that hold special meanings, like a space, (, or ). So for example I have a file named DSC06540 (New LCD TV).JPG this is an ugly filename but to move this file from my compact flash drive on my camera to my /home/max/images/electronics directory, I would simple escape all the special characters with a \ like this mv /media/CF/DSC06540\ \(New\ LCD\ TV\).JPG /home/max/images/electronics/
Another command I use quite often with the mv shell command is find. I can have find search for files on my computer and move the files to a new location. To do this there are 2 methods you can use, one method of using Linux find to move files is a small script, for example if I want to move all my php files to /home/max/htdocs/ I would run this: for i in `find *php`; do mv $i /home/max/htdocs; done.
Method 2 that uses Linux find command to move files in Linux is using the -exec option of find to execute mv. This command was brought to my attention a few days ago by another Linux user, and it is much faster than my method. Using the same example to move my php files, but using this method, you will run: find *php -exec mv {} /home/max/htdocs \; which is a lot less to type!
You can also change your find options any way you prefer to search your hard drive for the files you are looking for. Which I will go over in another Linux tutorial.
As you can see it isn’t hard at all to move files and folders in Linux shell prompt at all. As always I hope this Linux tutorial for beginners has helped you learn more about the Linux mv command. I hope you come back and find more help on Beginner Linux Tutorial.

No comments:

Post a Comment