LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 07-03-2011, 04:43 PM   #1
AndrewJS
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:

Code:
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:
Quote:
mv: cannot move `test1/4215/4215-001R.fit' to `test2/test.fit': 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
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
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.
Code:
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.

Markus

Last edited by markush; 07-04-2011 at 01:07 AM. Reason: made a mistake
 
Old 07-03-2011, 07:08 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
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')
Code:
#! /bin/bash
#
#	mv4sure.sh
#
#	Use me thusly:  
#
#		$ source mv4sure.sh
#		$ alias mv mv4sure
#		$ mv yadda/yadda/horty /blah/blah/florty


function mv4sure(){
mvSrc=$1
mvDest=$2

srcType="UNDEFINED"
destType="UNDEFINED"

	if [ -f $mvSrc ]; then

		echo "Source $mvSrc is a file"
		srcType="FILE"

	elif [ -d $mvSrc ]; then

		echo "Source $mvSrc is a dir"
		srcType="DIR"
	else
		echo "No such source"
		exit 1
	fi


	if [ -f $mvDest ]; then

		echo "Dest $mvDest is a file"
		destType="FILE"

	elif [ -d $mvDest ]; then

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

	fi



	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...."
			
			destDir=${mvDest%/*}
			srcFile=${mvSrc##*/}
			destFile=${mvDest##*/}
			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"
		fi
	fi
}
--- rod.
 
Old 07-03-2011, 07:20 PM   #4
Soadyheid
Senior Member
 
Registered: Aug 2010
Location: Near Edinburgh, Scotland
Distribution: Cinnamon Mint 17.0 at present.
Posts: 1,167

Rep: Reputation: 201Reputation: 201Reputation: 201
As far as I know,
Code:
mkdir
is the command to make a new directory. Then you can use
Code:
mv
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!
 
  


Reply


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


All times are GMT -5. The time now is 04:56 AM.

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