Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


ShellScript - Rename File Help
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

ShellScript - Rename File Help

Can someone help me write a bash/ shell script command to rename a cPanel Backup file to a new name? There are around 10 different files in a folder which needs to be renamed.

Original: backup-8.24.2016_17-39-13_.tar.gz
New: _temp_YYYYMMDD.tar.gz
YYYYMMDD needs to come from the original file name.

Thanks in advance.

Comments

  • jarjar Patron Provider, Top Host, Veteran

    It's been a while since I've restored a cPanel backup, but if memory serves... doesn't it freak out if your backup file isn't named in just the right way? You'd hate to have to rename them when you wanted to use them.

  • raindog308raindog308 Administrator, Veteran

    dwnewyork5 said: Can someone help me write a bash/ shell script command to rename a cPanel Backup file to a new name? There are around 10 different files in a folder which needs to be renamed.

    Show us your start on the problem and we'll help.

    Are you sure this isn't "can someone write a script for me that I can copy/paste"...

  • dwnewyork5 said: There are around 10 different files

    Just 10? This doesn't need a script for that imho.

  • @raindog308 said:

    dwnewyork5 said: Can someone help me write a bash/ shell script command to rename a cPanel Backup file to a new name? There are around 10 different files in a folder which needs to be renamed.

    Show us your start on the problem and we'll help.

    Are you sure this isn't "can someone write a script for me that I can copy/paste"...

    Will be honest. I have 0 clue with shell scripting . I need to rename files in a multiple folders before I archive them to ACD. Just don't wanna get flagged if using a standard name. Hence the help request.

  • djndjn Member
    edited December 2016

    find . -type f -name "backup*" -print0 | while read -r -d '' file; do

    mv "$file" "${file//backup/TEST}"

    done

    Will find all files that start with backup recursive and then change the text in the filename from backup to TEST

    Thanked by 1impossiblystupid
  • dwnewyork5 said: There are around 10 different files

    Just 10? This doesn't need a script for that imho.

    This exactly

  • http://pastebin.com/QdXT8fC3

    Then go php do.php (assuming do.php is the file with that code). Sure it's not bash but it IS technically a shell script when you run it from the CLI.

    Code is untested, written at the verge of getting drunk. Use at your own risk.

  • @definitelyliam said:
    Code is untested, written at the verge of getting drunk. Use at your own risk.

    Bringing PHP to an awk/sed party generally ensures drunkenness somewhere. ;)

    I'd start with djn's script, and just abuse cut on the filename to make it fit your request:
    cut -f1 -d_ | cut -f2 -d-

    Thanked by 2definitelyliam trewq
  • bsdguybsdguy Member
    edited December 2016

    I wouldn't do that on the fly but rather in vi (or whatever good editor you have) after piping the "ls backup-*" into some temp file.
    The tricky part is that you also want to flip the date order to yyyymmdd. That's easy with a regex.
    But there is another problem in that you don't want date item separators, which means you'd need to write some script to 0-fill the year, month, and day to 4 or 2 digits. As I assume that not having element separators isn't really important for you I suggest the following (example in vi):

    s:backup-\(\d\+\)\.\(\d\+\)\.\(\d\+\)_\(.\+\)\.tar\.gz:mv backup-\1.\2.\3_\4\.tar\.gz temp\3_\2_\1\.tar\.gz:g

    You may, of course also use sed or whatever but you should be aware of subtle differences in regex syntax.

    Once you are done you can save the file that now consists of lines like
    'mv backup-8.24.2016_17-39-13_.tar.gz _temp_2016_8_24.tar.gz'
    and run it through bash to actually rename your files as desired.

    P.S. Forgive my poor formatting. I don't know how to nicely format stuff here.
    P.P.S. That was a pain in the ass with this blog software. Unless someone tells me how to verbatim "code" format something this was the last time I helped with code here.

  • @WSS said:

    @definitelyliam said:
    Code is untested, written at the verge of getting drunk. Use at your own risk.

    Bringing PHP to an awk/sed party generally ensures drunkenness somewhere. ;)

    I'd start with djn's script, and just abuse cut on the filename to make it fit your request:
    cut -f1 -d_ | cut -f2 -d-

    Yep. I was so used to batch-renaming my linux ISOs this way, couldn't help it.

  • I'm guilty of doing the same if it's faster (and throwaway).

  • @dwnewyork5 said:
    Can someone help me write a bash/ shell script command to rename a cPanel Backup file to a new name? There are around 10 different files in a folder which needs to be renamed.

    Original: backup-8.24.2016_17-39-13_.tar.gz
    New: _temp_YYYYMMDD.tar.gz
    YYYYMMDD needs to come from the original file name.

    Thanks in advance.

    happy new year

    • shell script http://pastebin.com/v5D1Hejk and set BACKUPDIR variable to path to your backup directory containing backup-* tarballs
    • defaults to DEBUG=y which when run only echos old and new file names without actual renaming to doublecheck it's what you want
    • then for live run edit DEBUG=n and re-run script

    example DEBUG=y output

    ./convert.sh                        
    found backup-8.24.2016_17-39-13_.tar.gz
    -------------------------------
    debug mode output: 
    _temp_2016824
    17-39-13
    .tar.gz
    -------------------------------
    
    renaming backup-8.24.2016_17-39-13_.tar.gz to _temp_2016824.tar.gz ...
    -------------------------------
    debug mode output (echo only): 
    original file: /path/to/cpanel/backupdirectory/backup-8.24.2016_17-39-13_.tar.gz
    renamed file: /path/to/cpanel/backupdirectory/_temp_2016824.tar.gz
    

    example DEBUG=n output

    ./convert.sh                        
    found backup-8.24.2016_17-39-13_.tar.gz
    renaming backup-8.24.2016_17-39-13_.tar.gz to _temp_2016824.tar.gz ...
    mv /path/to/cpanel/backupdirectory/backup-8.24.2016_17-39-13_.tar.gz /path/to/cpanel/backupdirectory/_temp_2016824.tar.gz
    
  • Nice. Note, however, this -> "to _temp_2016824.tar.gz"

    What if the month was jan or feb? say 24.1.2016 ... then the result would be _temp_2016124.tar.gz ... which could mean dec. 4 or jan 24.

    That's why I included element separators to have a result like _temp_2016_1_24.tar.gz. If he/she doesn't like that one needed to 0-fill the elements.

  • yeah i just used the example @dwnewyork5 posted literally heh

  • @bsdguy fixed and updated original pastebin to check for single or double digit months

    with DEBUG=n set

    ./convert.sh    
    found backup-1.24.2016_17-39-13_.tar.gz
    renaming backup-1.24.2016_17-39-13_.tar.gz to _temp_20160124.tar.gz ...
    mv /path/to/cpanel/backupdirectory/backup-1.24.2016_17-39-13_.tar.gz /path/to/cpanel/backupdirectory/_temp_20160124.tar.gz
    found backup-8.24.2016_17-39-13_.tar.gz
    renaming backup-8.24.2016_17-39-13_.tar.gz to _temp_20160824.tar.gz ...
    mv /path/to/cpanel/backupdirectory/backup-8.24.2016_17-39-13_.tar.gz /path/to/cpanel/backupdirectory/_temp_20160824.tar.gz
    found backup-8.26.2016_17-39-13_.tar.gz
    renaming backup-8.26.2016_17-39-13_.tar.gz to _temp_20160826.tar.gz ...
    mv /path/to/cpanel/backupdirectory/backup-8.26.2016_17-39-13_.tar.gz /path/to/cpanel/backupdirectory/_temp_20160826.tar.gz
    found backup-8.25.2016_17-39-13_.tar.gz
    renaming backup-8.25.2016_17-39-13_.tar.gz to _temp_20160825.tar.gz ...
    mv /path/to/cpanel/backupdirectory/backup-8.25.2016_17-39-13_.tar.gz /path/to/cpanel/backupdirectory/_temp_20160825.tar.gz
    
    total 8.0K
    drwxr-xr-x 2 root root 4.0K Dec 31 14:03 .
    drwxr-xr-x 3 root root 4.0K Dec 28 17:34 ..
    -rw-r--r-- 1 root root    0 Dec 31 14:00 _temp_20160124.tar.gz
    -rw-r--r-- 1 root root    0 Dec 31 14:00 _temp_20160824.tar.gz
    -rw-r--r-- 1 root root    0 Dec 31 14:00 _temp_20160825.tar.gz
    -rw-r--r-- 1 root root    0 Dec 31 14:00 _temp_20160826.tar.gz
    -rw-r--r-- 1 root root    0 Dec 31 11:24 _temp_2016824.tar.gz
    -rw-r--r-- 1 root root    0 Dec 31 11:24 _temp_2016825.tar.gz
    -rw-r--r-- 1 root root    0 Dec 31 11:24 _temp_2016826.tar.gz
    

    happy new year !

  • 'date' is flexible and can do all kinds of formats:

    ls backup-*_.tar.gz | while read orig; do m1=${orig#*-}; m2=${m1%%_*}; m3=${m2//\./\/}; echo mv $orig $(date --date="$m3" "+_temp_%Y%m%d.tar.gz"); done

    Thanked by 1WSS
  • WSSWSS Member

    @Squyd said:
    'date' is flexible and can do all kinds of formats:

    ls backup-*_.tar.gz | while read orig; do m1=${orig#*-}; m2=${m1%%_*}; m3=${m2//\./\/}; echo mv $orig $(date --date="$m3" "+_temp_%Y%m%d.tar.gz"); done

    If you could guarantee GNU coreutils, it's all simple. However, that isn't always the case. ;) I'd also suggest quotes around the variables in case there is a space or other delimiter that would possibly cause chaos.

Sign In or Register to comment.