BASH - Files - Copy files to different directory

#!/bin/bash                                                                                                                                                                                                 
 
for filename in *; do
  if [[ -f "$filename" ]]; then
    base=${filename%.*}
    ext=${filename#$base.}
    mkdir -p "${ext}"
    mv "$filename" "${ext}"
  fi
done

Copy all files by extension to single directory

find . -name \*.txt -exec cp {} someDirectory \;

or

find . -name "file*" -exec mv {} /tmp \;

NOTE: The open brackets {} are a placeholder for the argument which is to be used from the output of find.

NOTE: Why not just use mv file* /tmp.

  • Problem with Moving Large Number of Files
    • Linux systems have a predefined limit on the maximum number of arguments that can be used with a single command.
    • This limit varies from system to system based on the stack size.
    • Thus, if a very high number of files are being specified with the wildcard; e.g. over a hundred thousand files, it throws an error: Argument list too long.