1

I'm working on a bash script file where I have a pre-generated list of PDF files which first get renamed per a CSV mapping, then I need to create a folder for each of them and then move them to it.

The first part I've got no problem with, but for the second, the directory for each file needs to named the same as the PDF filename, sans the .pdf suffix.

The way I'm (attempting) creating the directory names currently is by using the name of the PDF files after they've been renamed from the CSV.

Adding a column to my CSV with a mapping of the "proper" directory names isn't an option, so I can't use that as a workaround.

My current script is as follows:

#!/usr/bin/tcsh bash
sed 's/"//g' rename_list_authpers.csv | while IFS=, read orig new; do 
    mv "$orig" "$new"
    mkdir -p "$new"
done
echo 'Done.'

For reference, "$new" filenames are in the format: Prefix_Code#_Lastname.FirstName.pdf

I tried things like mkdir -p ${"$new"%.*} or even mkdir -p "$new"%.*, just to see if I could get a valid output even if the name itself gets cut off by the delimiter, but nothing works and I usually just get gibberish directory names.

EDIT: The reason the solution in the other thread wasn't working for me is that I was forgetting to call my script using bash instead of just ./script_name, so that solution will in fact work for this.

The fully working script now looks like this:

    #!/usr/bin/tcsh bash
    sed 's/"//g' list_lab_authpers.csv | while IFS=, read orig new num; do 
        mv "$orig" "$new"
    done
    echo 'Rename  Done.'
    for file in *.pdf; do
       mkdir "${file%.*}"
       mv "$file" "${file%.*}"
    done
    echo 'Directory creation done.'
EtherealBug
  • 165
  • 11

0 Answers0