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
    as a one-shot retroactive step to make directories and move existing files, or as a proactive step with some piece of software that's downloading these pictures? Commented Nov 9, 2018 at 17:19
  • This is curiously similar to Move files to a folder named by part of their filename, but that’s not for Unix&Linux. Commented Nov 10, 2018 at 5:10

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.