Moving Files

Hi There,

I am trying to move files, the file is present in this location:

/iAm4Free/test/generate/txt/information.txt

I need to move it to:

/iAm4Free/test1/generate/txt/information.txt

The only difference is the "test" is replaced with "test1". But the constraint is. The parent location the file exists in the same path specified but in the second location only /iAm4Free/test1/ exists but the remaining part "generate/txt/information.txt" dosen't exist. How can I create those directories automatically and move.

I know that mkdir -p creates directories, but is there something shorter in the mv command which does this automatically for you or any other simple way... any help i'll appreciate...

regards,
iAm4Free

Try this:
cd /iAm4Free
mkdir test1
cd test
cp -R * ../test1
# make the copy looks good
rm -rf *

The beauty of a Unix system is the ability to create new commands from existing tools. If there isn't a command that does exactly what you want, write one.

## NAME: mvd -- Move file to a (possibly none-existent) directory
## USAGE: mvd FILE ... DIR
## EXAMPLE: mvd /iAm4Free/test/generate/txt/information.txt /iAm4Free/test1/generate/txt

eval "destination=\$$#"
case $destination in
    */*) [ -d "$destination" ] || mkdir -p "$destination" || exit 1 ;;
esac
mv "$@"


Hey Johnson thanks... But I already submitted what I was supposed to do in a long way. I did a mv -f the filename from src and dest directories using the mkdir -p command to see if a directory dosen't exist. I did it in a longer way but I did like your logic of creating new commands :slight_smile: But I didn't quite understand what you have written... it looks Fundoo to me :slight_smile: Thanks..

Nicely done. Chris.

iAm4Free,
there's no better time than now to start learning from the best!

aah, but there is!
cpio

cd   /iAm4Free/test
find . | cpio -pd ../test1