D
D
Dmitry Petrik2013-04-29 09:40:05
linux
Dmitry Petrik, 2013-04-29 09:40:05

Bulk file rename script (Bash)?

They asked me just such a problem on Bash
b76c1a651bdbc6f38056aa124e0385d9.png
. I am not on friendly terms with Bash at all. Please let me know what command to use to solve this problem. And at least an approximate algorithm of work. And then he took it himself, heaped up the code for 2 pages and realized that I was moving somewhere in the wrong direction ...
Not for the faint of heart I tried to parse the file with the template and insert the result into the find command so that the necessary files were searched.

#!/bin/bash

target=$1
param=$2
file_rules=$3
param2=$4
file_log=$5
par3=$6

if [ $2 != "-r" ] ; then  #Проверяем параметр файла правил на валидность
        echo "Параметр не задан или задан не верно"
        exit
fi

if [ "$4" != "-l" -a -n "$4" ] ; then #Проверяем параметр логов на валидность
        echo "Параметр задан не верно"
        exit
fi

function parseFile {
cat $1 | while read line
do
#echo "$line"

IFS=' ' read -a array <<< "$line"
i=1
for element in "${array[@]}"
do
   # echo "$element"
    case "$i" in
        "1")
        fileName="-name ""\"$element\""
        ;;
        "2")
        replace="$element"
        ;;
        "3")
        if [ "$element" != "*" ] ; then
        userName="-user ""$element"
        else
        unset userName
        fi
        ;;
        "4")
        if [ "$element" != "*" ] ; then
        group="-group ""$element"
        else
        unset group
        fi
        ;;
        "5")
        if [ "$element" != "0" ] ; then
         size="-size ""\"$element\""
        else
        unset size
        fi
        ;;
    esac
    #items[i]="$element"
    #Some actions
    let i++
done
echo "find $2 -type f $fileName $userName $group $size -print"
find $2 -type f $fileName #$userName $group #$size -print
i=1

done
}

case "$6" in
"")
parseFile $3 $1
;;
"-c")
echo "param -c"
;;
"-ca")
echo "param -ca"
;;
*)
echo "Param is wrong"
;;
esac

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
Spencer, 2013-04-29
@Flexo

Wrote from nothing to do this script. Link to Patebin .
An example of a rules file (there may be comments and whitespace):

# jpeg -> jpg
.jpeg .jpg user group 200

foto photo * * *

'file with spaces ' 'file_non_spaces_' * * *

Call example:
$ ./massmv.sh ./files/ -r ./rules.txt -l ./log.txt

The log will be something like this:
Перемещён:  ./files/other_3.jpeg -> ./files/other_3.jpg
Перемещён:  ./files/foto_6.jpeg -> ./files/foto_6.jpg
Перемещён:  ./files/other_2.jpeg -> ./files/other_2.jpg
Перемещён:  ./files/foto_8.jpeg -> ./files/foto_8.jpg
И т.д.

You can also select options (mv - standard, cp, cp all):
$ ./massmv.sh ./files/ -r ./rules.txt -l ./log.txt -c  # cp
$ ./massmv.sh ./files/ -r ./rules.txt -l ./log.txt -ca # cp all

There will be questions - write :)

A
avalak, 2013-04-29
@avalak

The entire footcloth under the spoiler can be safely removed. For these purposes, theregetopts

#!/usr/bin/env bash

RULES=default.txt
TOGGLEME=false

while getopts "r:f" arg; do
  case $arg in
    r) RULES=$OPTARG ;;
    f) TOGGLEME=true ;;
    *);;
  esac
done

echo $RULES

+ findfor interacting with files.

N
Nikita Gusakov, 2013-04-29
@hell0w0rd

you need to make friends with find, grep
If you need to search in a file -cat file.txt | grep регулярка

S
Stepan, 2013-04-29
@L3n1n

stackoverflow.com/questions/4793892/recursively-rename-files-using-find-and-sed
See fing exec.
Find th search for regular expression, exec th rename.
Find can also search by all the parameters you need (owner, group, size ..).

V
Victor Taran, 2014-02-06
@shambler81

I agree, even you guys messed up there find + exec all you need, there is 1 line.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question