1

I have gone through various solution for renaming but none of them found fit in this situation. I have to rename a file oldfilename to newfilename with conditions like

  1. Prefix the hard coded string like

    "Pre_String" 
    
  2. followed by appending a part of old file name till any special character, if any

  3. followed by appending date that was calculated before like

    date = $(date +"%a, %d %b %Y %T %z)
    
  4. adding a custom extension like

    .abc
    
  5. followed by triming any space inbetween files created above that I did like

    newFileName = "Pre_String_$oldfilename_$date.abc" | tr -d ' '
    
  6. Finally i echo string

    echo "filename is $newFileName"
    
  7. and then rename the oldfilename to newFilename

    mv -v "$path$oldfilename" "$path$newFileName"
    

but even the simple one like above is not working. I did many changes but either it is taken as command not found or halflway string or mostly empty in echo. Also strangly enough the 7 command in most of hit and try gave the newFilename as exactly the oldfilename.

closed as off-topic by G-Man, Kiwy, Anthony Geoghegan, Jeff Schaller, Christopher Apr 25 '18 at 12:49

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Kiwy, Anthony Geoghegan, Christopher
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
  • Re: but even the simple one like above is not working., For variables assignments world not be space around = sign. Are those typo? Also in last step for testing use echo ... instead of mv directly, if it was your desired one then you can change it to mv. Another thing I see is wrong there newFileName = "Pre_String_$oldfilename_$date.abc" | tr -d ' ' which should be newFileName=$(echo Pre_String_$oldfilename_$date.abc" | tr -d ' ') – αғsнιη Apr 25 '18 at 3:59
  • 1
    Kindly post sample input and expected output – Praveen Kumar BS Apr 25 '18 at 4:21
  • 1
    @αғsнιη Thank you. Your correction worked like charm. With this I got food to work with, as I took *nix after almost 15 years. – AKS Apr 25 '18 at 6:39

Browse other questions tagged or ask your own question.