Thread: Move prefix files to folder by bash script
hi all,
want create script can me organize pictures folder. hope create script can gain new knowledge of using bash shell scripts
here problem:
files examples
what want put files start yyyy-mm folder name yyyy_mm,code:2011-12-29_10.37.40.jpg 2011-12-29_10.37.41.jpg 2012-01-02_00.56.59.jpg 2012-01-02_15.52.18.jpg 2012-01-02_15.52.19.jpg ....... .......
right manually by
as beginner of bash scripts. hope, can me..php code:
$ mkdir 2011_12 && mv 2011-12* 2011_12
$ mkdir 2012_01 && mv 2012-01* 2012_01
thanks.
you can use gnu/find find file names:
with -printf option can print yyyy-mm part of file names:code:find ./ -maxdepth 1 -regextype posix-extended -iregex '^\./[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}_.*.jpg'
now can pipe output of find gnu/sort or gnu/uniq in order omit repeated names:code:find ./ -maxdepth 1 -regextype posix-extended -iregex '^\./[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}_.*.jpg' -printf '%.9p\n'
and can redirect output of above pipeline while loop. in loop can create each directory , move files:code:find ./ ... | sort -u
please test script before use it. , keeping backup of data idea.code:#!/bin/bash # replace path/to/dir actual path cd path/to/dir || exit 1 while ifs= read -r -d '' dir mkdir -p "${dir//-/_}" mv -b -- "$dir"*.[jj][pp][gg] "${dir//-/_}" done < <(find ./ -maxdepth 1 -regextype posix-extended -iregex '^\./[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}_.*.jpg' -printf '%.9p\0' | sort -zu)
Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk Move prefix files to folder by bash script
Ubuntu
Comments
Post a Comment