Move a series of images into directories matching the image names

Snippet

I had a series of files (*.png) that I wanted moved into individual directories of the same name, sans extension.

# one line
for f in *png; do fn=$(basename $f); d="${fn%.*}"; echo $d; mkdir $d; mv $f $d; done
 
#
# Expanded with explanations
# 
 
# list of files
for f in *png; do
 
  # get the file name without the path
  fn=$(basename $f); 
 
  # get just the filename root, without the extension
  d="${fn%.*}"
 
  # output to screen so that we know something is going on
  echo $d
 
  # make the directory
  mkdir $d
 
  # move the file into the directory
  mv $f $d
 
# close the loop
done

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.