0

I have different files that their names contain the same pattern - 'part + (number)'. For example:

part 1.txt
part 2.txt

I want to create a folder for each file, to name the folder in the same name as the file, and to insert the file into his matching folder.

oguz ismail
  • 1
  • 16
  • 47
  • 69

1 Answers1

0

To move regular files with prefix part containing a suffix, you could do:

shop -s nullglob
for file in part*.*; do
  [ -f "$file" ] || continue
  dir=${file%.*}
  mkdir -p "$dir" && mv -i "$file" "$dir"
done

This enables Bash's shell options nullglob to expand non-matching patterns to a null string and the
test [ -f "$file" ] || continue skips non-regular files. The pattern ${file%.*} removes the suffix from the filename.
Option -p ignores an already existing directory and -i prompts if the file should already exist in the directory.

Freddy
  • 4,548
  • 1
  • 7
  • 17