flow
flow

Reputation: 63

How to match the string name to the file name?

I have a information list which looks similar to this

List.txt
dir1, filename1, 1990/01/01, 1995/01/09 10:35, john
dir2, filename2, 1994/12/08, 1994/12/10 05:43, jenny
dir3, filename3, 2000/07/03, 2005/10/11 14:56, Henrik

The first is directory, filename, creation time, modification time, owner of the file.

This list is very long. During the migration from mainframe to Linux, all the directories and files were migrated safely and the contents inside of them are the same, BUT the problem is that the modification time is changed to the time when it was migrated, so all the files have the modification time of the year 2018.

So my task is to bring back the modification time of the files as it was before the migration. So for this I have to create a script which will look at the List.txt and match the directory name and file name inside of it with the files which are on Linux, and if they are same, it has to change the modification time to the old one.

This script I tried to change the modification time for newing.txt file to the List.txt and it worked..

#!/bin/bash

aa=$1
while IFS="," read c1 c2 c3 c4 c5
do
        for n in $(pwd)
        do
                echo "$c1 $c2 $c3 $c4 $c5"

                y=$(echo $c4 | awk '{print $2}')
                b=$(echo $y | tr -d ':')
                echo $b
                c=$(echo $c4 | awk '{print $1}')
                againcommand=$(echo $c | tr -d '/')
                echo "$againcommand"
                abc=$againcommand$b
                echo $abc
                bde=`touch -t $abc "newing.txt"`
                echo $bde




        done
done < $aa

Any answers would be helpful!

Upvotes: 2

Views: 244

Answers (2)

David C. Rankin
David C. Rankin

Reputation: 84632

You can handle the updating of the file modification times simply using awk with its system command to invoke touch -d to update the file times from field 4 in the comma separated values file. For example:

awk -v FS=', ' '{ system("touch -d "$4 " " $1"/"$2) }' inputfile

Here awk reads each line with the awk variable for the field-separator set as FS=', ' (a comma and a space). It then invokes the system command calling touch -d, providing field 4 as the "date time" timestamp used for updating the file in the directory specified as $1/$2 (e.g. dir1/filename1).

Using your input file as inputfile it would be equivalent to calling:

touch -d 1995/01/09 10:35 dir1/filename1
touch -d 1994/12/10 05:43 dir2/filename2
touch -d 2005/10/11 14:56 dir3/filename3

The quoting within the system(...) command is provided to quote the non-field portions of the command. For "touch -d "$4 " " $1"/"$2, the command is then built using:

  • "touch -d " (e.g. touch -d and a space)
  • $4 (the 4th field)
  • " " (places a space between the end of the 4th field and beginning of dir/filename)
  • $1 (the 1st field - the dirname)
  • "/" (the separator between dirname/filename)
  • $2 (finally, the filename)

(note: you are essentially quoting everything that isn't an awk provided field)

You can do the same thing with chown and the 5th field to update the file ownership as well.

Upvotes: 1

Ron
Ron

Reputation: 6621

cat List.txt |while read z;do if [ -f $(echo $z|cut -d, -f1)/$(echo $z|cut -d, -f2|sed "s/^\s\+//g") ];then echo touch -t $(echo $z|cut -d, -f4|cut -d ' ' -f2|tr -d '/')$(echo $z|cut -d, -f4|cut -d ' ' -f3|tr -d ':') $(echo $z|cut -d, -f1)/$(echo $z|cut -d, -f2|sed "s/^\s\+//g") ;fi;done

you can remove the echo to execute the command. It will check if the file exists, and if it does, then it will apply the timestamp to it

Sorry, I just can't add the comments yet. So which echo I have to take and put this command instead? Thank you

Upvotes: 0

Related Questions