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.
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
dwnewyork5
Member
in General
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
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.
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"...
Just 10? This doesn't need a script for that imho.
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.
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
dwnewyork5 said: There are around 10 different files
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.
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-
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.
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).
happy new year
example DEBUG=y output
example DEBUG=n output
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
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
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.