-2

I'm looking for a script that can move files from one directory into another, however each file has a different name and its name corresponds to what folder it should be moved to.

For example:

  • file 1: var/www/testing/FileName_s800-date.zipvar/www/testing/old/s800/

  • file 2: var/www/testing/FileName_s801-date.zipvar/www/testing/old/s801/

(s800, s801 etc being the variant of said file and is what should define what folder the file goes into)

1
  • This question appears to be off-topic because Server Fault is not a free script writing service. Dec 23, 2014 at 21:47

1 Answer 1

1

This should do (note that this is untested):

#!/bin/bash

BASE_DIR="/var/www/testing"
NEW_BASE_DIR="/var/www/testing/old"
FILE_EXTENSIONS_TO_MOVE="zip"

# If the new directory doesn't exist, create it
if [ ! -d ${NEW_BASE_DIR} ] ; then
  echo "Creating ${NEW_BASE_DIR}"
  mkdir ${NEW_BASE_DIR}
fi

echo "Moving files with extension .${FILE_EXTENSION} in ${BASE_DIR} to ${NEW_BASE_DIR}"
# For each file in BASE_DIR (there is probably a better way to do this ...)
for FILE in $(ls -1 ${BASE_DIR} | grep ".${FILE_EXTENSION}"); do

  # First, get the file-name without extension
  FILE_NAME=${FILE%.*}
  NEW_FILE_EXTENSION=${FILE#.*}

  # Extract the variant
  PARTIAL_VARIANT=${FILE_NAME#_*}
  VARIANT=${PARTIAL_VARIANT%-*}

  # Extract the file's base name (before the underscore)
  BASE_NAME=${FILE_NAME%_*}

  # Extract the date (after the dash)
  FILE_DATE=${FILE_NAME#-*}

  # Now build the new path and file-name
  NEW_DIR="${NEW_BASE_DIR}/${VARIANT}"
  NEW_FILE_NAME="${BASE_NAME}_${VARIANT}_${FILE_DATE}.${NEW_FILE_EXTENSION}"

  # Test if the directory exists
  if [ ! -d ${NEW_DIR} ]; then
    echo "Creating directory ${NEW_DIR}"
    mkdir ${NEW_DIR}
  fi

  # Finally, move the file ...
  echo "Moving ${BASE_DIR}/${FILE} to ${NEW_DIR}/${NEW_FILE_NAME}"
  mv ${BASE_DIR}/${FILE} ${NEW_DIR}/${NEW_DIR}/${NEW_FILE_NAME}"

done

Input:

  • /var/www/testing/FileName_s800-YYYYMMDD.zip
  • /var/www/testing/FileName_s801-YYYYMMDD.zip
  • /var/www/testing/FileName_s802-YYYYMMDD.zip

Output:

  • /var/www/testing/old/s800/FileName_s800_YYYYMMDD.zip
  • /var/www/testing/old/s801/FileName_s801_YYYYMMDD.zip
  • /var/www/testing/old/s802/FileName_s802_YYYYMMDD.zip
9
  • Hi, thanks for the quick reply. However what if the mv was two directories in? For example. file 1: var/www/testing/kernel1.zip > var/www/testing/old/kernel1/ file 2: var/www/testing/kernel2.zip > var/www/testing/old/kernel2/
    – user261035
    Dec 23, 2014 at 16:55
  • @user261035 - apologies, I've edited my script. It needs to test for the existence of the directory, and create it if it doesn't exist. Dec 23, 2014 at 16:58
  • thank you. Although I've found the issue here being I over simplified my file names in reality they're actually something like this: FileName_s800-date.zip, FileName_s801-date.zip With the s800 and s801 being the thing that defines which folder the file should go to.
    – user261035
    Dec 23, 2014 at 17:07
  • Well, we can only suggest a suggestion if you give us the correct information. When you edit your original question with the correct requirements, I'll revise my script and edit my answer, but I can't do much more without knowing your full requirements. Which portion of the filename do you want in the new directory name etc? Dec 23, 2014 at 17:08
  • Ah sorry, I've edited the post.
    – user261035
    Dec 23, 2014 at 17:10

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