Find the answer to your Linux question:
Results 1 to 3 of 3
What I want to do is write a script that will rename a file, possibly by appending a date or time, when I open it for editing. If I open ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux User
    Join Date
    Jan 2005
    Location
    Florida
    Posts
    414

    how to write a filename script?


    What I want to do is write a script that will rename a file, possibly by appending a date or time, when I open it for editing. If I open the file just to read it, without editing, then the name should remain the same.
    This is just an idea that occurred to me as I have a couple of classes that will require me working on the same files multiple times throughout the semester and I thought it would be handy if I could just write a script for it instead of renaming each file manually as I edit (I'm hopeless at keeping track of my work and I always seem to overwrite something I shouldn't)
    registered linux user: 387197

  2. #2
    Linux Guru dylunio's Avatar
    Join Date
    Aug 2004
    Location
    Cymru
    Posts
    4,157
    Code:
    #!/bin/bash
    
    # make a copy of the file in /tmp
    cp $1 /tmp/
    
    # start your editor
    $EDITOR $1
    
    DIFF=$(diff $1 /tmp/$1)
    
    # If the output of diff is 1 (edited) then change the filename
    if [ -n DIFF ]; then
    mv $1 $1-`date +"%T"`-`date +"%d"`-`date +"%m"`-`date +"%y"`
    fi
    
    #remove copy in /tmp
    rm /tmp/$1
    Try that, I'm not 100% if it'll work, but it'll give you an idea for sure.
    Registered Linux User #371543!
    Get force-get May The Source Be With You
    /dev/null
    /dev/null2

  3. #3
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Alternatively, you could just set up a local Subversion repository to store your files in. That way, you won't have to worry about overwriting stuff you shouldn't (at least not as long as you check in your work properly).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •