Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
Please explain your current experience with BASH scripting for the benefit of those looking to assist you.
Please make an attempt at writing a BASH script to start with your problem and post where you are with this.
If you have no BASH scripting experience, there are several links in my signature which can help you to get started, go more advanced, and assist you with debugging BASH scripts.
Your problem definition is fairly clear.
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:
Anything commands you can type in a command line, you can write into a BASH script.
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
for f in $(find /working -type f -name *.mkv); do
newdir=$(echo "$f" | grep -Eo '[0-9]{8}')
if [ ! -d "/workingnew/$newdir" ]; then
mkdir -p /workingnew/$newdir
fi
rsync -vah --progress $f /workingnew/$newdir/
done
Last edited by szboardstretcher; 11-08-2016 at 01:24 PM.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.