1

I need to create a report of all files renamed with the ff fields in the CSV formatted report (Output.rpt).I need an output that displays

a.  Column1 = Original Filename
b.  Column2 = Original Timestamp date
c.  Column3 = Renamed filename value

below is my code for x number of files that i have created with the original file name.

for i in {1938..2037}; do   
    ## create a file with a random month
    touch -d "${i}-$((RANDOM % 12 + 1))-01" file_$((i-1937))
    done
Output is below:
-rw-r--r-- 0 Oct  1  2037 file_100
-rw-r--r-- 0 Jul  1  2036 file_99
-rw-r--r-- 0 Sep  1  2035 file_98
-rw-r--r-- 0 Jan  1  2034 file_97

Below is my script for renaming the files with timestamps

for f in *
do
  ref=$(stat -c %y "$f" | awk '{print $1}')
  mon=$(date -d "$ref" +%b)
  year=$(date -d "$ref" +%Y)
  mv -- "$f" "file_${mon^^}${year}"
done

Output:

    -rw-r--r-- 0 Oct  1  2037 file_OCT2037
    -rw-r--r-- 0 Jul  1  2036 file_JUL2036
    -rw-r--r-- 0 Sep  1  2035 file_SEP2035
    -rw-r--r-- 0 Jan  1  2034 file_JAN2034
  • 2
    I see you've created several questions leading up to this. You cannot get the previous file name after it's been renamed, so you should clarify the current working environment for this question. We must assume that the files have not yet been renamed. – Jeff Schaller Sep 22 '17 at 21:44
0

For actual renaming, remove the echo word.

ls -l file_* | 
awk '
BEGIN {
    OFS=",";
    print "Original filename", "Original timestamp", "Renamed filename" > "output.rpt";
}
{
    renamed_filename = "file_" toupper($6) $8; 
    original_timestamp = sprintf("%d-%s-%02d", $8, $6, $7);
    exit_status = system("echo mv -v -- " $9 " " renamed_filename);
    if(!exit_status)
        print $9, original_timestamp, renamed_filename > "output.rpt";
}'

Original files

-rw-rw-r-- 1 user user   0 Jul  1  1938 file_1
-rw-rw-r-- 1 user user   0 Sep  1  1947 file_10
-rw-rw-r-- 1 user user   0 May  1  2037 file_100

Generated renaming commands

mv -v -- file_1 file_JUL1938
mv -v -- file_10 file_SEP1947
mv -v -- file_100 file_MAY2037

output.rpt

### Comma-separated values.
$ cat output.rpt
Original filename,Original timestamp,Renamed filename
file_1,1938-Jul-01,file_JUL1938
file_10,1947-Sep-01,file_SEP1947
file_100,2037-May-01,file_MAY2037

### Pretty printed by the `column` command.
$ cat output.rpt | column -t -s,
Original filename  Original timestamp  Renamed filename
file_1             1938-Jul-01         file_JUL1938
file_10            1947-Sep-01         file_SEP1947
file_100           2037-May-01         file_MAY2037
  • @MiniMax..thank you for the feedback..However not sure why im getting this error mv: cannot stat '1940': No such file or directory and no result for the output.rpt, it only shows the header Original filename Original timestamp Renamed filename..hoping for ur reply. – trixie101 Sep 25 '17 at 16:57
  • @trixie101 This error happens when you remove echo? What output you get with echo? What renaming commands generated? In my answer they are: mv -v -- file_1 file_JUL1938, mv -v -- file_10 file_SEP1947, so on. – MiniMax Sep 25 '17 at 17:42
  • yes after i removed echo it gives me that error. when I run it with echo it gives me these results mv -v -- 1941 file_01, mv -v -- 1943 file_01 and so on all shows file_01. then on rpt i got Original filename Original timestamp Renamed filename 1941 1-0-00 file_01 1943 1-0-00 file_01 1947 1-0-00 file_01 – trixie101 Sep 25 '17 at 17:54
  • @trixie101 What output this command: ls -l file_01? It seems, that your ls -l file_01 output is different from my. My is: -rw-rw-r-- 1 user user 0 Jul 1 1938 file_1. It have 9 columns. The filename file_1 is the ninth column. – MiniMax Sep 25 '17 at 18:06
  • @trixie101 Also, try add backslash in the beginning of code: \ls -l file_* | awk .... A leading backslash prevents both aliases and builtins from being used. May be it will help. – MiniMax Sep 25 '17 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.