arranging files
Hi Folks,
I have an Axis video server that produces mkv files, but those mkv files are deeply scattered in subdirectories. There is a pattern in the filenames, each file begins with the date: 20111108_XYZA_121212.mkv , 20111109_XYDA_121A32.mkv etc. I'd like to write a bash script that does 2 thing: - makes directories: 20111108, 20111109 - moves files into the corresponding directory I don't know the good approach for this job. Could you give me some ideas? I've come across this so far to find out the directory names, but I don't know how to assign this into an array: find . -type f -name "*.mkv" | grep -Eo '[0-9]{8}' | sort | uniq This arranging function is buggy and not elegant: https://github.com/kerzol81/Bash-and...nger_from_axis Thanks, Zoli |
Member Response
Some recommendations:
Note which commands you would use in a terminal to accomplish what you want the script to do. Follow my personal, golden BASH script rule: Quote:
|
mkdir, find -exec mv ...
|
basically this one does the job, but I think this is still buggy:
#!/bin/bash LOCAL_FOLDER="$HOME/axis" TO_FOLDER="$HOME/axis_arranged" EXTENSION='mkv' function arranging(){ mkdir -p $TO_FOLDER cd $TO_FOLDER || exit 1 for i in `find $LOCAL_FOLDER -name *.$EXTENSION | grep -Eo '[0-9]{8}' | sort | uniq`; do if [ ! -d $i ]; then mkdir -p $i mv -u `find $LOCAL_FOLDER -name $i*.mkv` $i else mv -u `find $LOCAL_FOLDER -name $i*.mkv` $i fi done } function main(){ arranging } main |
Seems overly complicated. I don't see a reason to 'sort' them.
I did not use 'mv' to MOVE the files, because I always thing it is best to COPY them first, then delete the old when you are done. So I used 'rsync' which gives you a status indicator. This script will look in /working for any files with extension mkv, grab the 8-digit number from them, make the 8-digit number directory, then copy the file to that directory. Feel free to modify it to MOVE the files, but beware. Code:
#!/bin/bash |
Thank you. Rsync is great. Much better that what I came up with. This way if source file gets overwritten, rsync increments.
But this way I'll need double space :-) Anyway, thank you for your help. |
You can break apart strings in bash. In this case, something like "${i%%_*}" will grab the first part of the file
eg Code:
a="20111108_XYZA_121212.mkv" Code:
new_dir=foobar; while read i; do echo mkdir -p "$new_dir${i%%_*}"; echo cp "$i" "$new_dir""${i%%_*}$i"; done < <(find $PWD -type f -iname '*_*.mkv') Code:
new_dir=foobar |
Just for fun, an alternative bash solution.
Code:
#!/bin/bash |
thanks
|
All times are GMT -5. The time now is 12:27 AM. |