Answer the question
In order to leave comments, you need to log in
Bulk file rename script (Bash)?
They asked me just such a problem on Bash
. 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
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_' * * *
$ ./massmv.sh ./files/ -r ./rules.txt -l ./log.txt
Перемещён: ./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
И т.д.
$ ./massmv.sh ./files/ -r ./rules.txt -l ./log.txt -c # cp
$ ./massmv.sh ./files/ -r ./rules.txt -l ./log.txt -ca # cp all
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
find
for interacting with files.
you need to make friends with find, grep
If you need to search in a file -cat file.txt | grep регулярка
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 ..).
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 questionAsk a Question
731 491 924 answers to any question