Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 07-03-2011, 04:43 PM   #1
LQ Newbie
Registered: Dec 2010
Posts: 16

Rep: Reputation: 0
points about the 'mv' command

I want to copy a file from an old directory to a new directory and ideally I would like that the 'mv' command itself could create the directory for me in one step...
That is to say that the directory would not exist until 'mv' has been called as in:

mv olddirectory/file.dat newdirectory/file.dat
so mv would create the directory 'newdirectory' in the above call....

I am not sure if this is possible however, that is to say I think it may be necessary that the directory 'newdirectory' already exists before I make the above call to mv, as when I make try the above call when 'newdirectory' doesn't exist I get the following error:
mv: cannot move `test1/4215/' to `test2/': No such file or directory
so to sum up, my question is:
is there some paramater for 'mv' that I can change so that it will create the new directory as well as copy the file and if not, are there other commands that might do this?
Old 07-03-2011, 05:04 PM   #2
Senior Member
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hello AndrewJS,

mv doesn't rename directories! (Edit: it can rename but not rename and move simultaneously)

I'd recommend for such often used commands like mv, read the manpage.
man mv
mv moves files or whole direcory-trees to another place in your filesystem. If you want to move your file.dat to a new directory, this directory has to be created before moving the file. Read the manpage for the mkdir command, this command has some interesting options which will help you (together with the mv) command to do what you've described.


Last edited by markush; 07-04-2011 at 01:07 AM. Reason: made a mistake
Old 07-03-2011, 07:08 PM   #3
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,398
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
You can use mv to rename directories, but only if they are the most specific part of the filespec.

Perhaps this bit of shell script provides somewhat of a solution, as well as an illumination of the problem that mv simply avoids (this has undergone only cursory testing. If you use it for real, test thoroughly, and then remove the 'echo' commands to 'go live')
#! /bin/bash
#	Use me thusly:  
#		$ source
#		$ alias mv mv4sure
#		$ mv yadda/yadda/horty /blah/blah/florty

function mv4sure(){


	if [ -f $mvSrc ]; then

		echo "Source $mvSrc is a file"

	elif [ -d $mvSrc ]; then

		echo "Source $mvSrc is a dir"
		echo "No such source"
		exit 1

	if [ -f $mvDest ]; then

		echo "Dest $mvDest is a file"

	elif [ -d $mvDest ]; then

		echo "Dest $mvDest is a dir"
		echo "Dest $mvDest doesn't exist. This will get confusing."


	if   [ $srcType == 'FILE' -a $destType == 'FILE' ]; then
    		echo mv $mvSrc $mvDest

	elif [ $srcType == 'FILE' -a $destType == 'DIR' ]; then
    		echo mv $mvSrc $mvDest

	elif [ $srcType == 'DIR' -a $destType == 'DIR' ]; then
    		echo mv $mvSrc $mvDest

	elif [ $srcType == 'DIR' -a $destType == 'FILE' ]; then
    		echo "Huh? You can't move a DIR to a FILE"

	elif [ $destType == 'UNDEFINED' ]; then
		if [ $srcType == 'DIR' ]; then
			#  Assume a non-existing destination is a directory....
			echo mkdir -p $mvDest
			echo mv $mvSrc $mvDest
		elif [ $srcType == 'FILE' ]; then
			#  But, if the source is a FILE, then should 
			#  we make the full directory and move the file there, 
			#  or does the destination specify a directory 
			#  component and a filename component?
			echo "I really don't know what to do here...."
			echo "I could make $destDir and move the "
			echo "file '$srcFile' there with the name '$destFile'"
			echo "or I could make $mvDest as a directory, and move the file there"
--- rod.
Old 07-03-2011, 07:20 PM   #4
Senior Member
Registered: Aug 2010
Location: Near Edinburgh, Scotland
Distribution: Cinnamon Mint 17.3 (Netbook) and 18.3 (Desktop)
Posts: 1,408

Rep: Reputation: 334Reputation: 334Reputation: 334Reputation: 334
As far as I know,
is the command to make a new directory. Then you can use
to move a file from it's original directory to the one you've just made.

You could probably write a script to allow you to have one command to create the directory and move the file, you'd still have to supply the new directory and file names, but if you're only doing it once, why bother?

Play Bonny!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
What are the COMMAND LINES for MOUNT POINTS on GENTOO 2007? linuxfreak247 Linux - Newbie 1 01-08-2011 08:20 AM
play free (World of WarCraft , xBox Live, Live Points, Wii Points, Free Habbo) laraaj Linux - Games 1 02-09-2007 06:50 PM
Looking for access points. al912912 Linux - Wireless Networking 6 02-11-2005 06:34 PM
Mount Points? Mr_Floppy4 Linux - General 2 10-14-2002 11:03 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:34 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration