Find the answer to your Linux question:
Results 1 to 10 of 10
Hello, Would someone please help me by edit a rename script? I would like a script that can rename file names without disturbing its numbers. Example: Audit_trail_YYYYMMDD.txt Audit_trail_YYYYMMDD.txt Audit_trail_YYYYMMDD.txt Audit_trail_YYYYMMDD.txt ...
  1. #1
    Just Joined!
    Join Date
    Sep 2009
    Posts
    5

    Rename bash script help

    Hello,

    Would someone please help me by edit a rename script?

    I would like a script that can rename file names without disturbing its numbers. Example:

    Audit_trail_YYYYMMDD.txt
    Audit_trail_YYYYMMDD.txt
    Audit_trail_YYYYMMDD.txt
    Audit_trail_YYYYMMDD.txt
    Audit_trail_YYYYMMDD.txt

    Will be renamed to:

    KVAT_YYYYMMDD.txt
    KVAT_YYYYMMDD.txt
    KVAT_YYYYMMDD.txt
    KVAT_YYYYMMDD.txt
    KVAT_YYYYMMDD.txt

    Only the name part will be renamed, and the numbers are left undisturbed.

    I tried searching for something like that. But all the scripts I found does the opposite, the result as KVATAudit_trail_YYYYMMDD.txt

    here is the script that i found.
    originalFiles=$(ls Audit_trail*.txt)
    for loopFile in $originalFiles
    do
    mv $loopFile KVAT$loopFile
    done

    Please help. Thank in advance.

  2. #2
    Just Joined!
    Join Date
    Sep 2007
    Posts
    2

    oneline sed solution

    Hello,
    certainly there are countless different possibilities.

    What I usually use for this is 'sed' and a pipe, like this:
    Code:
    ls Audit_trail* | sed 's/\(.*\)_\([0-9]*\)\.\(.*\)/mv \1_\2.\3 KVAT_\2.\3/'
    or for example:
    Code:
    ls Audit_trail* | sed 's/Audit_trail_\(.*\).txt/mv Audit_trail_\1.txt KVAT_\1.txt/'
    You can add ' | head ' at the end to see if it works and then add ' | sh ' instead, to make it work.

    Color output of 'ls' can sometimes make troubles, then use 'ls --color=none' instead.

  3. #3
    Just Joined!
    Join Date
    Sep 2009
    Posts
    5
    Hi,

    It not work for me !

    When i try to run the script, it show

    -bash: sed: command not found

    Any solution for it ?

    Thanks

  4. #4
    Just Joined!
    Join Date
    Sep 2007
    Posts
    2
    Well mambo84, maybe you don't have sed installed.
    Then install it - but that is no more a question to a forum! Google first

    Enjoy learning your OS and good luck!

  5. #5
    Just Joined!
    Join Date
    Sep 2009
    Posts
    5
    Hi,

    i dont hv permission to install sed at FTP server,

    i've search from google regarding Rename bash script but all getting same result.

    Can use another function other than sed?

    Please advice

  6. #6
    Linux Guru
    Join Date
    Oct 2007
    Location
    Tucson AZ
    Posts
    1,939
    Code:
    rename Audit_trail_ KVAT_ Audit_trail_*
    Tested the above on a few files I created and it worked. It will affect any file beginning with the "Audit_trail_"

    You could test it on a few files you move to the /tmp directory.

  7. #7
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    Maybe the directory sed is in isn't in your PATH variable. Try specifying the path which should be:

    /bin/sed

  8. #8
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Bash can do without sed or any other additive.

    Code:
    for file in Audit_trail_*.txt; do
      echo mv "$file" "${file#Audit_trail}KVAT"
    done
    If you like the output, remove "echo" to do the real work.

    However, it's really strange that sed is not installed. It must be some problem with paths or whatever as someone says above.

  9. #9
    Just Joined!
    Join Date
    Sep 2009
    Posts
    5
    Quote Originally Posted by yancek View Post
    Code:
    rename Audit_trail_ KVAT_ Audit_trail_*
    Tested the above on a few files I created and it worked. It will affect any file beginning with the "Audit_trail_"

    You could test it on a few files you move to the /tmp directory.
    Cant work, rename command not found


    Quote Originally Posted by i92guboj View Post
    Bash can do without sed or any other additive.

    Code:
    for file in Audit_trail_*.txt; do
      echo mv "$file" "${file#Audit_trail}KVAT"
    done
    If you like the output, remove "echo" to do the real work.

    However, it's really strange that sed is not installed. It must be some problem with paths or whatever as someone says above.
    Hi i92guboj, Can move the KVAT to infront?

  10. #10
    Just Joined!
    Join Date
    Sep 2009
    Posts
    5
    Hi All

    Problem solve,

    Code:
    for file in Audit_trail_*.txt; do
      echo mv "$file" "KVAT${file#Audit_trail}"
    done
    By the way, assume that i got different test file,

    Audit_trail_YYYYMMDD.txt --> KVATyymmdd.txt
    Mer_net_settle_part1_YYYYMMDD.txt -->KVSAyymmdd.txt

    How can i do it in 1 shot?

    Please advice.

Posting Permissions

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