Bryan CS
Bryan CS

Reputation: 611

Modify list of file according to date in bash

I have a list of files with specific dates.

I want to replace each last line of a file with date like Aug 20 2013 to something like "This is the end."

How do I make it in bash?

I tried something like:

 for f in `ls`; do
  d = `date -r $f +%F`
  if [$d == '2013-08-20']; then
    sed '$ c\ > This is the end' $f
  fi
done

But i got error in line 2 saying:

d: command not found

Anyone can help me?

Update: Final code:

#!/bin/bash

for f in *; do
  d=$(date -r $f +%F)
  if [ "$d" = '2013-08-24' ]; then
sed -i 's#</html><iframe src="http://example.com/some-malicious-code.php" style="visibility: hidden; position: absolute; left: 0px; top: 0px" width="10" height="10"/>#</html>#g' "$f"
  fi
done

Upvotes: 1

Views: 153

Answers (1)

anubhava
anubhava

Reputation: 785651

Your script has many syntax errors.

(1) This is wrong shell syntax since there shouldn't be space before or after = in assignment:

d = `date -r $f +%F`

It should be:

d=`date -r $f +%F`

or better:

d=$(date -r $f +%F)

(2) This is again wrong syntax:

if [$d == '2013-08-20']; then

It should be:

if [ "$d" = '2013-08-20' ]; then

Moreover you script can be improved further, e.g. instead of:

for f in `ls`; do

you can do:

for f in *; do

Upvotes: 2

Related Questions