DotrainExpress
DotrainExpress

Reputation: 9

Moving files/creating folders shell script

I'm attempting to move files in a directory to another directory based on file names. File names will be COMPANY-ReportType_timestamp.

I'm currently using this to make the directories:

for i in *
        do mkdir /mnt/folder/"${i%-*}"
           mkdir /mnt/folder/"${i%-*}"/"${i%_*}"
done

and using this to move the files:

for i in *
        do mv $i /mnt/folder/"${i%-*}"/"${i%_*}"
done

In this script, it will create the "COMPANY" folder and then "COMPANY-ReportType" folder within it. If I could parse the "Company-" from the subfolder, that'd be great. However, I was just given a twist on this whole process.

Sometimes, they won't provide a specific ReportType and will simply have COMPANY-blahblahtimestamp. In this case, they want it moved to the root of the COMPANY folder. Any ideas on how to do this??

I appreciate the help on this! I've tried a few variations but still not quite right. Hoping if I put a little more info it will help...:

Files I have to move:

Company-Report1_yyyymmdd Company-Report2_yyyymmdd Company-Filenameyyyymmdd Company-Filename-yyyymmdd

The outcome I need is a Company folder created, if it doesn't already exist. Then folders for Report1 and Report2 created under the Company and then the Report1 and Report2 files moved into their folders. Then move the two files without the _ into the root of the Company folder.

Again, thank you so much for your help on this! I appreciate it! (and thank you for pointing out what your variables are doing so I can better understand them!)

Upvotes: 0

Views: 888

Answers (2)

BMW
BMW

Reputation: 45243

Here is the fix (it can handle more deep subfolders)

for i in *
do 
  folder=${i%_*}                          # folder = COMPANY-ReportType
  folder="/mnt/folder/${folder//-//}"     # folder = /mnt/folder/COMPANY/ReportType
  mkdir -p "${folder}"     # mkdir with -p option: no error if existing, make parent directories as needed exist or not.
  mv "$i" "${folder}"      
done

New edit after new request:

for i in *               # sample as: Company-Report1_yyyymmdd Company-Report2_yyyymmdd Company-Filenameyyyymmdd Company-Filename-yyyymmdd
do
  if [[ "$i" =~ _ ]]; then                   # check if the file name has "_" or not.
     folder=${i%_*}                          # folder = COMPANY-ReportType
     folder="/mnt/folder/${folder//-//}"     # folder = /mnt/folder/COMPANY/ReportType
  else
     folder="/mnt/folder/${i%%-*}"           # folder = /mnt/folder/COMPANY
  fi

  mkdir -p "${folder}"     # mkdir with -p option: no error if existing, make parent directories as needed exist or not.
  mv "$i" "${folder}"
done

mkdir -p /mnt/folder/Company/Report1
mv Company-Report1_yyyymmdd /mnt/folder/Company/Report1
mkdir -p /mnt/folder/Company/Report2
mv Company-Report2_yyyymmdd /mnt/folder/Company/Report2
mkdir -p /mnt/folder/Company
mv Company-Filenameyyyymmdd /mnt/folder/Company
mkdir -p /mnt/folder/Company
mv Company-Filename-yyyymmdd /mnt/folder/Company

Upvotes: 1

Raul Andres
Raul Andres

Reputation: 3806

Name your variables

for i in *
do
        company=${i%-*}
        rest=${i#*-}
        type=${rest%_*}
        name=${rest#*_}
        if [ $type -eq $name]; then # No report type
             mkdir -p /mnt/folder/$company
             mv $i /mnt/folder/$company/$name
        else
             mkdir -p /mnt/folder/$company/$type
             mv $i /mnt/folder/$company/$type/$name
        fi
done

Upvotes: 0

Related Questions