LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-08-2016, 01:18 PM   #1
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Mint
Posts: 100

Rep: Reputation: Disabled
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

Last edited by kzo81; 11-08-2016 at 01:55 PM. Reason: more ideas
 
Old 11-08-2016, 01:35 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,444
Blog Entries: 13

Rep: Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762Reputation: 3762
Member Response

Some recommendations:
  1. Please explain your current experience with BASH scripting for the benefit of those looking to assist you.
  2. Please make an attempt at writing a BASH script to start with your problem and post where you are with this.
  3. 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.
 
1 members found this post helpful.
Old 11-08-2016, 01:37 PM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,281

Rep: Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916
mkdir, find -exec mv ...
 
Old 11-08-2016, 02:18 PM   #4
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Mint
Posts: 100

Original Poster
Rep: Reputation: Disabled
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
 
1 members found this post helpful.
Old 11-08-2016, 02:21 PM   #5
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,237

Rep: Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653
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 02:24 PM.
 
1 members found this post helpful.
Old 11-08-2016, 02:58 PM   #6
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Mint
Posts: 100

Original Poster
Rep: Reputation: Disabled
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.

Last edited by kzo81; 11-08-2016 at 03:51 PM.
 
Old 11-08-2016, 06:34 PM   #7
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 607

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
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"
echo "$a" "${a%%_*}" 
20111108_XYZA_121212.mkv 20111108
Allowing you to do something like this:

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')
Formatted
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')
After you verify the file/dirnames are correct, remove the echos
 
2 members found this post helpful.
Old 11-09-2016, 07:41 AM   #8
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,366

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Just for fun, an alternative bash solution.
Code:
#!/bin/bash

EXTENSION=".mkv"
LOCAL_FOLDER="$HOME/axis"
TO_FOLDER="$HOME/axis_arranged"

function arranging(){

mkdir -p $TO_FOLDER
cd $LOCAL_FOLDER || exit 1

shopt -s globstar
for i in **/*$EXTENSION; do
  [[ "$i" =~ ([[:digit:]]+)_.+$EXTENSION ]] \
    && [ ! -d "$TO_FOLDER/${BASH_REMATCH[1]}" ] \
    && mkdir -p "$TO_FOLDER/${BASH_REMATCH[1]}"
  cp -u  "$i"  "$TO_FOLDER/${BASH_REMATCH[1]}"
done
shopt -u globstar
}

arranging

Last edited by allend; 11-09-2016 at 07:45 AM.
 
Old 03-19-2017, 04:45 PM   #9
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Mint
Posts: 100

Original Poster
Rep: Reputation: Disabled
thanks

Last edited by kzo81; 03-19-2017 at 04:55 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Arranging files in a directory linux_ Linux - Newbie 8 06-08-2012 01:11 PM
Arranging Files In Alphabetical order swatward Linux - General 4 12-11-2006 09:14 PM
Re-arranging a hard disk silkenphoenixx Linux - Software 5 10-13-2006 02:27 PM
help with re-arranging hard disk partitions umichLinux Linux - General 4 04-10-2005 05:13 PM
Partition re-arranging, Imaging and Linux 1kyle Linux - Software 0 03-17-2004 04:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration