Change file's numbers Bash

前端 未结 2 1260
闹比i
闹比i 2021-01-27 12:21

I need to implement a script (duplq.sh) that would rename all the text files existing in the current directory using the command line arguments. So if the command duplq.sh

相关标签:
2条回答
  • 2021-01-27 12:52

    A possible solution...

    #!/bin/sh
    
    NUMBERS=$(ls $1|sed -e 's/pic//g' -e 's/\.txt//g'|sort -n -r)
    
    for N in $NUMBERS
    do
        NEW=$(($N + 3))
        echo "pic$N.txt -> pic$NEW.txt"
        mv "pic$N.txt" "pic$NEW.txt"
    done
    
    0 讨论(0)
  • 2021-01-27 13:18

    find possible files, sort the names by number, use sed to remove files with lower numbers than $1, reverse sort by number, and start at the top renaming from the highest number down to the lowest:

    #!/bin/sh
    find . -maxdepth 1 -type f -name "$1"'[0-9]*' | 
    sort -g | sed -n "/^$1$2."'/,$p' | sort -gr | 
    while read x ; do
        p="${x%%[0-9]*.*}"; s="${x##*[0-9]}" ; i="${x%$s}" i="${i#$p}"
        mv "$x" "$p$((i+$3))$s"
    done 
    
    0 讨论(0)
提交回复
热议问题