Skip to main content

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
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  .......  .......
what want put files start yyyy-mm folder name yyyy_mm,
right manually by

php code:
mkdir 2011_12 && mv 2011-122011_12
mkdir 2012_01 && mv 2012-012012_01 
as beginner of bash scripts. hope, can me..

thanks.

you can use gnu/find find file names:

code:
find ./ -maxdepth 1 -regextype posix-extended -iregex '^\./[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}_.*.jpg'
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' -printf '%.9p\n'
now can pipe output of find gnu/sort or gnu/uniq in order omit repeated names:
code:
find ./ ... | sort -u
and can redirect output of above pipeline while loop. in loop can create each directory , move files:

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)
please test script before use it. , keeping backup of data idea.


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk Move prefix files to folder by bash script


Ubuntu

Comments

Popular posts from this blog

Some mp4 files not displaying correctly (CS6)

Thread: Samba is not authenticating with LDAP