2

On /Desktop/a/ , there is a set of files:

DSCN1840.AVI00001.png
DSCN1840.AVI00002.png
DSCN1840.AVI00003.png
DSCN1841.AVI00001.png

What I would like is to have the following structure:

Desktop/a/DSCN1840/

  DSCN1840.AVI00001.png

  DSCN1840.AVI00002.png

  DSCN1840.AVI00003.png

Desktop/a/DSCN1841/

  DSCN1841.AVI00001.png
2

1 Answer 1

2

As a one-shot step, you could loop through all of the files and gather the desired subdirectory name; if that subdirectory doesn't already exist, create it; then move that file into that subdirectory:

for f in *.png
do
  subdir=${f%%.*}
  [ ! -d "$subdir" ] && mkdir -- "$subdir"
  mv -- "$f" "$subdir"
done

The key part of this is the parameter expansion in ${f%%.*}, which says to remove the longest matching portion (%%) of a period followed by anything, which effectively strips off, for example, the .AVI00003.png portions of the filenames, leaving only the leading directory indicator.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.