Hi, is there a way to create a directory when its non-existent when trying to move files to this particular folder?
Thanks much.
Hi, is there a way to create a directory when its non-existent when trying to move files to this particular folder?
Thanks much.
mkdir -p ${DIRECTORY}
Thanks skymart. if I have to include this to a script, I just need to add this line or do I need to change something?thanks
Why dont you try it out? Add an if condition first to check that the directory name doesn't exist.
OK
$ [ ! -d directory ] && mkdir -p directory || echo "mv files directory/"
Remove the echo
part after testing ..
@jayan, if there is no directory, then this will create the directory, but then it will not mv the files to it..
My bad
$ move="mv file* directory"
$
$ echo $move
mv file* directory
$
$ touch file1 file2 file3 file4 file5
$
$ ls -ltr directory
directory: No such file or directory
$
$ [ ! -d directory ] && mkdir -p directory | eval $move || eval $move
$
$ ls -ltr directory/
total 0
-rw-rw-rw- 1 userid gid 0 Apr 9 10:15 file5
-rw-rw-rw- 1 userid gid 0 Apr 9 10:15 file4
-rw-rw-rw- 1 userid gid 0 Apr 9 10:15 file3
-rw-rw-rw- 1 userid gid 0 Apr 9 10:15 file2
-rw-rw-rw- 1 userid gid 0 Apr 9 10:15 file1
$
How about:
[ -d testdir ] || mkdir -p testdir && echo "mv files testdir"
But perhaps something like this might be a bit easier to understand..
if [ ! -d testdir ]; then
mkdir -p testdir
fi
mv files testdir