I have one directory with files in different formats: .txt, .TextGrid, .csv and I want to split the directory in a manner in which each directory contains files of specific format. Like one directory for .txt, one for .TextGrid etc.

  • mkdir txt; mv *.txt txt/? Should do the job
    – Panki
    Jun 3, 2020 at 12:16

1 Answer 1


Loop over the files. For each file, calculate the name of the destination directory based on the file name. Create the directory if it doesn't exist already, and move the file there.

I assume that you're satisfied that the “format” of a file is determined by its extension. The code below won't move files without an extension (e.g. wibble) or dot files (e.g. .foo.bar).

set -e                   # Abort on an error
for file in *.*; do      # Loop over file names that have an extension, excluding those that start with a dot
  dir="${file##*.}"      # Take the file's extension (we know there is one because the file name matches *.*)
  mkdir -p -- "$dir"     # Create the directory if it doesn't exist already
  mv -- "$file" "$dir/"  # Move the file into the directory

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .