简体   繁体   中英

Want to run .sh Linux shell Scripting to rename file

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM