We do not have to test for individual directory level 'cos if the bottom directory exists, the parent level should exist. Also, mkdir -p will make all the non-existing parent directories. Also, I introduce 'short circurt' && to ensure directory exist before I move the file.
We can also avoid all the repeating code using in extracting yy/mm/dd/hh by using 'set --' and sed. sed will change 2 digits with 2 digits + space so that it can put the result back to "set --" to set the positional variables accordingly
This is my contribution, it should work (even on sh)
Code:
for i in *.jpg
do
# yy is $1, mm is $2, dd is $3, hh is $4
set -- `echo $i | sed -e 's/\([0-9][0-9]\)/\1 /g'`
dir="$1/$2/$3/$4"
[ ! -d $dir ] && mkdir -p $dir && mv $i $dir
done
Your script is absolutely perfect, I love it ... except ...
the last --> [ ! -d $dir ] && mkdir -p $dir && mv $i $dir <-- wouldnt actually move the file if the directory existed already, so I added an extra line below with just --> mv $i $dir <-- in place and that solved that part, so it now becomes, although I would imagine this will now generate an error if the directory it wants to create doesnt exist (but shouldnt error for subsequent files - I think)
Code:
#!/bin/bash -x
for i in *.jpg
do
# yy is $1, mm is $2, dd is $3, hh is $4
set -- `echo $i | sed -e 's/\([0-9][0-9]\)/\1 /g'`
dir="$1/$2/$3/$4"
[ ! -d $dir ] && mkdir -p $dir && mv $i $dir
mv $i $dir
done
I was trying to be 'smart', too many 'short circuit' make me short circuit too.
it should look like this:
Code:
for i in *.jpg
do
# yy is $1, mm is $2, dd is $3, hh is $4
set -- `echo $i | sed -e 's/\([0-9][0-9]\)/\1 /g'`
dir="$1/$2/$3/$4"
[ ! -d $dir ] && mkdir -p $dir
mv $i $dir
done
You may even want to ensure *.jpg to be your patten by:
Code:
for i in [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].jpg
do
...
Not sure whether bash has a more elegant way to pick up your files in the for loop, but the above [0-9]... should definitely work for sh.
As for danmero contribution, I couldn't get it working in Solaris bash
Code:
$ i=090807060504.jpg
$ d=${i:0:8}
bad substitution
$ echo $SHELL
/bin/bash
$ uname -a
SunOS chihung 5.10 Generic_118833-36 sun4u sparc SUNW,UltraSPARC-IIi-cEngine
In cygwin, although it does not throw exception, the variable d is equivalent to extracting the first 8 digits. We still need to turn that into directory path before we can make the hierarchical tree structure
I've got this script to loop through all folders and move files that are more than 2 years old. I'm using the install command because it creates the necessary directories on the destination path and then I remove the source. I'd like to change the script to use the mv command since it is much... (4 Replies)
how can i move "dataName".sql.gz into a folder called 'database' and then move "$fileName".tar.gz * .htaccess into a folder called 'www' with the entire gzipped file being "$fileName".tar.gz? Is this doable or overly complex.
so
mydemo--2015-03-23-1500.tar.gz
> database
-... (5 Replies)
I am very new to BASH and I am having difficulties moving a long list of image files into similarly named directories. I've been trying to come with a script all night and no luck. Here is what my list of files looks like:
DSC_0059_01.jpg
DSC_0059_02.jpg
DSC_0059_03.jpg
DSC_0059_04.jpg... (5 Replies)
im trying to move media and other files which are in a specified directory to another directory and create another one if it does not exits(where the files will go),them also create a directory will the remaining files with different extensions will go.my first problem is that my script is not... (8 Replies)
I am fairly new to bash(but am proficient in C++), and have only completed a few simple scripts. This is my first script that I actually need to do a serious task.
All of my audiobooks are stored in traditional MP3 format: Music/Artist/Album/*.mp3 (which in this case is... (0 Replies)
hi
i have a list of directory in a text file with all directories name in a column.(this is not exactly a file but i need to do a grep and awk on a file to find that list)
i have the source folders like
abchome/abc/xxyz/nl_xxabc/mm// v01
... (4 Replies)
Hi,
I have 500 directories each with multiple data files inside them. The names are sort of random. For example, one directory has files named e_1.dat, e_5.dat, e_8.dat, etc. I need to move the files to a single directory and rename them all in numerical order, from 1.dat to 1000(or some... (1 Reply)
Hi All,
I am in a tricky situation where I have to move my files to a different mount point and create a link in place of the file which will point to the moved location.
to explain you in details:-
say I have two mount points
/dir/mount1/
/dir/mount2/
I have my application... (5 Replies)
I want to connect to an SFTP server, GET some files, then move those files to a different directory on the SFTP server so I don't try to GET them next time. But there doesn't seem to be a way to move files between directories on the remote server from SFTP. I missing something obvious? And if... (6 Replies)
Hi,
Can someone help me with creating a bash shell script.
I need to create a script that gets a positive number n as an argument.
The script must create n directories in the current directory with names like map_1, map_2 etcetera. Each directory must be contained within its predecessor. So... (7 Replies)