1

Trying to automate some work stuff and this includes many folder generations but and I was thinking of using a dynamic array to do this, let me know how this sounds.

First Part of my Script is to export a directory list using the:

ls /home/USER/Downloads/ > "/home/USER/Desktop/scripts/FolderList.txt"

and it gives the output as expected:

Folder With Some Long Title
Folder With Another Long Title

In this example I only gave (2) but there could be upwards of 1500+ folders and I have already written the commands to create the other folders on my server and name them automatically based off the original folder name, but the problem I have is I want my scripts to index thru all the folders and preform my commands (there are about 8 that are mostly string manipulations and such). So I was thinking of getting the count of the number of items in the Directory then write them to the text file mentioned above, then using a FOR-Loop to run that number of times that will contain my 8 or so commands? any thoughts or better ideas? I Can get the count using:

wc -l FolderList.txt | awk '{ print $1 }'

But I'm not sure how to create or manage a Dynamic Array and use it with the for-loop to index thru my commands.

here is some of the script that goes with it currently some commented out for modular testing:

#TestString="Customer Info (YEAR) [PMNAME] [OWNERNAME] [EETYPE]"
cd "/home/USER/Downloads/"
ls /home/USER/Downloads/ > "/home/USER/Desktop/scripts/FolderList.txt"
wc -l FolderList.txt | awk '{ print $1 }'

#CUSTINFO=${TestString/(*)*}
#YEAR=$(grep -o '(.*)' <<<"$TestString")
#RESO3=$(grep -o '\[.*\]' <<<"$TestString")
#RESO2=${RESO3//\[PMNAME\]/}
#RESO_FINAL=${RESO2//\[EETYPE\]/}
#FULL_NAME=$TITLE_FINAL$CUSTINFO$YEAR
#mv "/home/USER/Downloads/$DOWNLOAD_NAME" "/home/USER/Downloads/$FULL_NAME"
#for f in */* ;do fp=$(dirname "$f"); ext="${f##*.}" ; mv "$f" "$fp"/"$fp"."$ext" ;done
  • all functionality built into script since the read location is always the same. so just {./script.sh} – user354662 May 30 '19 at 4:35
  • So the script has hardcoded foldernames? – muru May 30 '19 at 4:36
  • yes, since the read location is always the same, i copy folders form my win pc to my linux VM to run this – user354662 May 30 '19 at 4:37
  • i added the majority of my commands that i use to generate the new folder names and such there is more that is still in my testing but all of the commands i posted are working – user354662 May 30 '19 at 4:52
  • so the TestString here is supposed to be the name of a directory in /home/USER/Downloads/, or a line in FolderList.txt? – muru May 30 '19 at 4:54
0

Going by the commands and comments, it looks like what you want to do is:

for TestString in /home/USER/Downloads/*
do 
    #CUSTINFO=${TestString/(*)*}
    #YEAR=$(grep -o '(.*)' <<<"$TestString")
    #RESO3=$(grep -o '\[.*\]' <<<"$TestString")
    #RESO2=${RESO3//\[PMNAME\]/}
    #RESO_FINAL=${RESO2//\[EETYPE\]/}
    #FULL_NAME=$TITLE_FINAL$CUSTINFO$YEAR
    #mv "/home/USER/Downloads/$DOWNLOAD_NAME" "/home/USER/Downloads/$FULL_NAME"
    #for f in */* ;do fp=$(dirname "$f"); ext="${f##*.}" ; mv "$f" "$fp"/"$fp"."$ext" ;done
done

The outer for loop loops over files in /home/USER/Downloads/ (by wildcard expansion), setting TestString to each filename in turn.

| improve this answer | |
  • Awesome worked great, i think i was way over thinking it with the export and import haha. THANK YOU @muru – user354662 May 30 '19 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.