Skip to main content
1 of 3
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

Assuming the names of the files and directories follow the same naming convention in that they share some common grouping prefix followed by a dot, and assuming we don't know the filename suffixes of files or directories:

topdir=.

for dirname in "$topdir"/*/; do
    prefix=$( basename "$dirname" )
    prefix=${prefix%%.*}

    for filename in "$topdir/$prefix".*; do
        if [ ! -d "$filename" ]; then
            mv -i "$filename" "$dirname"
        fi
    done
done

The outer loop iterates over all directories in the directory $topdir (here set to ., the current directory). The prefix will be the base name of the directory name, with the bit after the first dot removed.

Once the prefix has been computed, non-directories (files) in the same $topdir directory that share the same prefix are moved to the directory.

Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k