Bash and Awk for creating directories and moving files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash and Awk for creating directories and moving files
# 8  
Old 07-24-2008
Quote:
Originally Posted by chihung
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

# 9  
Old 07-24-2008
Quote:
Originally Posted by danmero
Shorther:
Code:
for i in *.jpg;do d=${i:0:8};test -d $d || mkdir $d ;mv $i $d;done

Awesome single line version, perfect except the lack of nested directories.

Instead of a single directory 08072511 I need them nested like

>08
->07
-->25
--->11
# 10  
Old 07-24-2008
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
Code:
$ i=090807060504.jpg
$ d=${i:0:8}
$ echo $d
09080706
$ uname -a
CYGWIN_NT-5.1 chihung 1.5.25(0.156/4/2) 2007-12-14 19:21 i686 Cygwin

Danmero, did I miss anything
# 11  
Old 07-24-2008
Ops, fast reading Smilie
Code:
for i in *.jpg;do d=.$(sed 's/\(..\)/\/\1/g' <<< ${i:0:8});test -d $d || mkdir -p $d ;mv $i $d;done

@chihung
Code:
$ uname -a
Linux test 2.6.18-6-686 #1 SMP Sun Feb 10 22:11:31 UTC 2008 i686 GNU/Linux


Last edited by danmero; 07-24-2008 at 11:22 PM..
# 12  
Old 07-24-2008
Try...
Code:
for i in *.jpg; do d=$(echo $i|cut -c-8|fold -2|paste -s -d /); mkdir -p $d; mv $i $d; done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need BASH Script Help to Move Files While Creating Directories

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)
Discussion started by: consultant
4 Replies

2. Shell Programming and Scripting

Archiving and moving files into directories, creating directories, etc.

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)
Discussion started by: wyclef
5 Replies

3. Shell Programming and Scripting

Need Help Moving Long List Of Files Into Directories

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)
Discussion started by: jowens1138
5 Replies

4. Shell Programming and Scripting

moving files to different directories

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)
Discussion started by: elginmulizwa
8 Replies

5. Shell Programming and Scripting

Moving files from several directories into parent

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)
Discussion started by: gamendorf
0 Replies

6. Shell Programming and Scripting

moving files between directories !!

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)
Discussion started by: debu000
4 Replies

7. UNIX for Dummies Questions & Answers

Moving files out of multiple directories and renaming them in numerical order

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)
Discussion started by: renthead720
1 Replies

8. Shell Programming and Scripting

moving files and creating a link to it

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)
Discussion started by: rpraharaj84
5 Replies

9. UNIX for Dummies Questions & Answers

Moving files between directories using SFTP

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)
Discussion started by: cjhancock
6 Replies

10. Shell Programming and Scripting

help needed with creating challenging bash script with creating directories

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)
Discussion started by: I-1
7 Replies
Login or Register to Ask a Question