Find the answer to your Linux question:
Results 1 to 4 of 4
I am doing a simple backup Bash script that creates a backup archive of a folder (or file) I am working on. I want to have snapshaps all saved to ...
  1. #1
    Just Joined!
    Join Date
    May 2007
    Posts
    2

    generating unique file names (bash)

    I am doing a simple backup Bash script that creates a backup archive of a folder (or file) I am working on. I want to have snapshaps all saved to one directory. To ensure unique name, I am currently appending the date and time to "MyArcName_data_time." But this creates a long and ugly name. I'd like either something cleaner like a unique number appended (based on total files in directory), or some random temp filename generator function that I don't yet know about.

    The count would be sufficient, as each folder will always hold ONLY these archives, and no hidden files.

    Thus, my directory would be:

    myArcName_1.tar.gz
    myArcName_2.tar.gz
    myArcName_3.tar.gz
    ...

    iF there's some bash function for this, groovy. If not, i just need to know how to count the directories from inside my script.

    Thanks in advance

  2. #2
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52
    Quote Originally Posted by Shoq View Post
    I am doing a simple backup Bash script that creates a backup archive of a folder (or file) I am working on. I want to have snapshaps all saved to one directory. To ensure unique name, I am currently appending the date and time to "MyArcName_data_time." But this creates a long and ugly name. I'd like either something cleaner like a unique number appended (based on total files in directory), or some random temp filename generator function that I don't yet know about.

    The count would be sufficient, as each folder will always hold ONLY these archives, and no hidden files.

    Thus, my directory would be:

    myArcName_1.tar.gz
    myArcName_2.tar.gz
    myArcName_3.tar.gz
    ...

    iF there's some bash function for this, groovy. If not, i just need to know how to count the directories from inside my script.

    Code:
    n=
    name=whatever
    tarfile=$name.tgz
    while [ -f "$tarfile" ]
    do
      n=$(( ${n:=0} + 1 ))
      tarfile=$name-$n.tgz
    done

  3. #3
    Just Joined!
    Join Date
    May 2007
    Posts
    2
    Doh!

    Now why didn't that occur to me. Burn out is an ugly thing. Thanks!

  4. #4
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Smile

    I think you could also use....mktemp(thanks to coderoot )
    man mktemp
    http://www.linuxforums.org/forum/lin...ile-names.html
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

Posting Permissions

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