Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9191733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:45:24+00:00 2026-06-17T20:45:24+00:00

I would like to run a .sh script to rename a file which is

  • 0

I would like to run a .sh script to rename a file which is in the directory desktop/reports/don of my computer. An example of what I need is to rename:

TACOS_2013-Jan-22__00-50-00_UTC.csv 

to

TACOS_20130122_005000.csv

I have the following script which was created using windows batch script(.bat file). I would like to convert this into linux shell script.

@echo off
setlocal
for /f "tokens=2-7 delims=_.-" %%A in ('dir /B TACOS_*') do (
  setlocal enabledelayedexpansion
  call :getmonth %%B
  ren TACOS*_*%%A-%%B-%%C*_*%%D-%%E-%%F_UTC.csv TACOS_%%A!mon!%%C_%%D%%E%%F.csv
  endlocal
)


:getmonth
if "%1" equ "Jan" set mon=01
if "%1" equ "Feb" set mon=02
if "%1" equ "Mar" set mon=03
if "%1" equ "Apr" set mon=04
if "%1" equ "May" set mon=05
if "%1" equ "Jun" set mon=06
if "%1" equ "Jul" set mon=07
if "%1" equ "Aug" set mon=08
if "%1" equ "Sep" set mon=09
if "%1" equ "Oct" set mon=10
if "%1" equ "Nov" set mon=11
if "%1" equ "Dec" set mon=12
goto :eof
endlocal

this is what i have done so far..Please help

#!/bin/bash
month["Jan"]=01
month["Feb"]=02
month["Mar"]=03
month["Apr"]=04
month["May"]=05
month["Jun"]=06
month["Jul"]=07
month["Aug"]=08
month["Sep"]=09
month["Oct"]=10
month["Nov"]=11
month["Dec"]=12

directory="desktop/reports/Don/"
for path in "${directory}TACOS_"*; do

    path=${path#${directory}}
    newpath=${path:0:10}${month[${path:11:3}]}${path:15:2}
    newpath=${newpath}__$(tr -d '-' <<< ${path:19:8}).csv

    echo "${directory}${path}" "${directory}${newpath}" # Run this one first!!!
    # mv "YOUR/PATH/${path}" "YOUR/PATH/${newpath}"
done
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:45:25+00:00Added an answer on June 17, 2026 at 8:45 pm

    Now that you made it a bit clearer, I guess this is what you want:

    • an associative array to map “MONTH NAME” to “MONTH NUMBER”;
    • rename a file from “TACOS_YYYY-month-dd__HH-MM-SS*.csv” to “TACOS_YYYYmmdd_HHMMSS.csv”.

    The solution in a bash script:

    #!/bin/bash
    declare -A month
    month["Jan"]=01
    month["Feb"]=02
    month["Mar"]=03
    month["Apr"]=04
    month["May"]=05
    month["Jun"]=06
    month["Jul"]=07
    month["Aug"]=08
    month["Sep"]=09
    month["Oct"]=10
    month["Nov"]=11
    month["Dec"]=12
    
    directory="YOUR/PATH/TACOS_"
    for path in "${directory}"*; do
    
        path=${path#${directory}}
        newpath=${path:0:4}${month[${path:5:3}]}${path:9:2}
        newpath=${newpath}__$(tr -d '-' <<< ${path:13:8}).csv
    
        echo "${directory}${path}" "${directory}${newpath}" # Run this one first!!!
        # mv "YOUR/PATH/${path}" "YOUR/PATH/${newpath}"
    
    done
    

    This converts the string path=TACOS_2013-Jan-22__00-50-00_UTC.csv into newpath=TACOS_20130122__005000.csv, and renames the initial file mv‘ing it to the new path constructed.

    As in explanation, bash offers you associative arrays, that you have to declare prior to any operation using declare -A assoc_array.

    In bash you can take string intervals, setting an offset, a length, and doing ${string:offset:length}. Concatenation is performed by juxtaposition of strings, and assignments must have no spaces between left_value=right_value.

    In addition, you have tr command, translating your string from initial to initial_without_characters, since the flag -d has been used. You may take a look at man tr for further reference.

    Edit:

    Since you don’t have a more recent version of bash, you can use the following code:

    #!/bin/bash
    function month() {
    
        case $1 in
            "Jan") echo "01" ;;
            "Feb") echo "02" ;;
            "Mar") echo "03" ;;
            "Apr") echo "04" ;;
            "May") echo "05" ;;
            "Jun") echo "06" ;;
            "Jul") echo "07" ;;
            "Aug") echo "08" ;;
            "Sep") echo "09" ;;
            "Oct") echo "10" ;;
            "Nov") echo "11" ;;
            "Dec") echo "12" ;;
        esac
    
    }
    
    directory="YOUR/PATH/TACOS_"
    for path in "${directory}"*; do
    
        path=${path#${directory}}
        newpath=${path:0:4}$(month ${path:5:3})${path:9:2}
        newpath=${newpath}__$(tr -d '-' <<< ${path:13:8}).csv
    
        echo "${directory}${path}" "${directory}${newpath}" # Run this one first!!!
        # mv "YOUR/PATH/${path}" "YOUR/PATH/${newpath}"
    
    done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to run a nose test script which accepts
I would like to run a shell script, from a file or from an
I would like to run a shell script(.sh) from a Java class. Which is
I would like to use JRuby to run a script which populates a database.
Now I have the rename script like this: cd /Users/KanZ/Desktop/Project/Test/ n=1 for file in
I would like to run this script (embed drive list in a site) that
I would like to run a script in production. This script generates a record
I would like to run a script indefinitely. It look likes my current script,
I would like to run a script after files with certain extensions are saved
I would like to suppress output in R when I run my R script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.