1

I need to grade some source code from various students. The submission process is through Blackboard which appends to the file name a bunch of unnecessary stuff. Here is an example:

Test Submission_adunn_attempt_2014-04-04-10-48-37_adunn-helloworld.tar.gz

I would like to rename this file (and all others) to:

adunn-helloworld.tar.gz

How can I do this in a Bash script?

2
  • What is the logic for trimming?
    – devnull
    Apr 5, 2014 at 2:20
  • I only want everything after the final underscore.
    – Daeden
    Apr 5, 2014 at 4:06

1 Answer 1

3

You could use shell parameter expansion:

$ s="Test Submission_adunn_attempt_2014-04-04-10-48-37_adunn-helloworld.tar.gz"
$ echo "${s##*_}"
adunn-helloworld.tar.gz

Use a loop for renaming the files:

for f in *.gz; do
  mv "$f" "${f##*_}"
done

Alternatively, you could use prename (a part of perl distribution):

rename 's/.*_//' *.gz
2
  • +1. Note, however, that this script does not check for duplicates.
    – dawud
    Apr 5, 2014 at 11:09
  • Thank you for your response. This is exactly what I needed. I was actually fairly close with something similar to '${f##*_}', but I do not fully understand the syntax for this type of operation.
    – Daeden
    Apr 5, 2014 at 19:16

You must log in to answer this question.

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