[JIRA] Renaming Attachments to their Original File Names


JIRA caters for attachments a bit more differently now than what it used to be pre-JIRA 4.2. I believe this could be because attachments in ID format would eliminate the possibility of encoding issues over various file systems (when moving from Windows to Linux, or vice-versa for example).

However on some case, users would wish to have their file attachments retain their original file names. This could be due to audit or transferring purposes.

Remember the earlier post about reading CSV files using Bash scripting, and tokenizing them? I used that and whipped up a script that would be useful for users to implement this renaming feature.

  1. Before starting the execution of the script, run the following SQL query, and save the result as CSV:
    select id, filename from fileattachment where issueid in (select id from jiraissue where pkey like 'YOUR_PROJECT_KEY%');

    replace YOUR_PROJECT_KEY with the project that you would like to make the changes.

  2. Open the CSV file in your editor to remove the header, remove the no of rows at the end, and setting the delimeter to ‘,':

      id   |                                                                       filename
     37417 | test1.docx
     35964 | test2.docx
     35758 | test3.docx
     35708 | test4.docx
    (3 rows)


  3. Create a new file called convert.sh and place the following (replace where applicable; refer to the comments in the script):
    # Here is where you insert the path of a project's attachment
    # normally it's best to copy the entire <PROJECT_KEY> folder to not tamper
    # with existing data.
    # adjust the values in the loop {1..925} to cater for the first directory to the last one
    # in the setting below, it is set to start from TEST-1 to TEST-925
    for i in {1..925}
        # this would change the directory to a specific issue key, based on the loop above
        # e.g: cd /home/jalex/Desktop/TEMP/TEST/TEST-1
        cd $MAIN/TEST-$i
        # let's start looping the files in the <ISSUE_KEY>-NUM directory
        for f in $MAIN/TEST-$i/*
            # stripping the full path from the result above; just leaving the fileID
            echo "Processing $filename file..."
            # start reading the CSV file, delimiting by ',' symbol 
            while IFS=, read col1 col2
                # if the current fileID is similar to the current ID in the CSV, continue
                if [ $filename = "$col1" ]
                    # rename the file to its correspondence "real name"
                    cp $filename "$col2"
            # the location of your CSV file. Using absolute path is recommended.
            done < /home/jalex/Desktop/TEMP/TEST/attachment.csv
        # just acting as a separator when we are processing sub-directories
        echo "-----"
        # make sure we change our directory back to the <PROJECT_KEY> level
        cd ..
  4. Make sure the script is executable:
    sudo chmod +x convert.sh
  5. Run the script:
    sh convert.sh
  6. If there are a lot of attachments, this might take a while. Go grab a coffee and relax! :)

Was That a Good Read? Rate It!

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Add a Comment

Your email address will not be published. Required fields are marked *

Current day month ye@r *

CommentLuv badge