Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

4 Problem Suppose you have a directory full of phone pictures and each of them h

ID: 3597457 • Letter: 4

Question

4 Problem Suppose you have a directory full of phone pictures and each of them has a name of the form: 20171225_204539.jptg where the first part is a date, the second part is just random numbers and the extension is the type of image. Here is a command line bash script. 1-1; for N in *.jpg; do x=$ (echo "$N" I sed-e 'blah'); mv "$N" "$x"; (($i++)); done replace the blah in the above so that it changes the filenames to be of the form xmas-20171225-1.jpg xmas-20171225-2.jpg xmas-20171225-3.jpg etc.

Explanation / Answer

Given below is the modified script

#!/bin/bash

i=1;
for N in *.jpg;
do
x=$(echo "$N" | sed -e "s/([0-9]*).*/xmas--$i.jpg/");
mv "$N" "$x";
((i++));
done

===============

This was tested with bash shell. The sed command is explained below

sed -e "s/([0-9]*).*/xmas--$i.jpg/"

We use s to substiute pattern with some replacement. The syntax is s/pattern/replacement/

Since the filenames begin with 1 or more digits followed by underscore, we use ([0-9])* to match all the digits occuring before _. This matched pattern group is numbered 1 by sed which can be back referenced using . The replacement string is xmas--$i.jpg

 refers to the matched group and $i is value of the variable i which is incrementd in the loop.

Please do rate the answer if it helped. Thank you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote