Može li mi neko napisati Bash skriptu koja pronađe sve fajlove na serveru pod istim imenom i zamjeni sa nekim drugim.
Npr ako na mašini postoji više fajlova (u različitim folderima) pod nazivom “test.py”, da ova skripta pronađe te fajlove i zamjeni ih sa drugim fajlom “test_novi.py”.
#!/bin/bash
#30.dec.2012 bash file exchanger, die7
#v0.1
# Function will make a backup of founded files
back_oldf(){
touch $HOME/backup-$file.tar
for i in $found; do tar -rf $HOME/backup-$file.tar 2>/dev/null $i;done
backup_tar=$(ls -l $HOME/backup-$file.tar)
}
#In the first step function will catch a directory target for founded files
#In next step it will copy a new file to targets
#Last step it will remove old files
dir_ops(){
dir_t=$(for i in $found; do dirname $i;done)
mv_newf=$(echo $dir_t | xargs -n 1 cp $newfile)
rm_oldf=$(for i in $found; do rm $i;done)
}
run_it_dude(){
clear
echo "Please enter a full path where the search it for: "?
read targ
echo "Please enter a correct name for file you wish find/exchange: "?
read file
echo "where is stored new file, please enter a full path/name: ?"
read newfile
echo "-------------------------------------------------------------------------------------------------------------------------------- "
echo "Planed operation is to find file whit a name $file in the target $targ and to replace it with the $newfile, is this correct: y/n ?"
read answ
if [ "$answ" = y ];
then
#Search Result depending on the given target
found=$(find $targ -name $file)
echo -e " founded files are\n $found ,\n are realy sure you wish to proceed: y/n?"
read answ2
if [ "$answ2" = y ];
then
echo "-----------------------------------------------"
echo "creating backup started"
back_oldf
echo "for the security purposes backup is created and stored in $backup_tar, you can remove it after check!"
echo ""
echo "------------------------------------------------"
echo " files exchanged, enjoy :)!"
dir_ops
elif [ "$answ" = n ];
then
echo "No way I am out from here!!!"
exit
else
echo "Quite and close"
exit
fi
fi
}
#opleti rodjak:)
run_it_dude
eto to gore sejfaj, onda Xchmodiraj pa opleti
to bi bilo to :), sa ovim script mozes traziti/mjenjati filove koje hoces , gdje hoces sa cim hoces i jos ti napravi backup, posto je ovo odradjeno u 15min svaka elegancija iskljucena, ko hoce nek mijenja ili dadne konstruktivne predloge za daljnja poboljsanja
zli duhovi u mojoj glavi kazu sa perlom bi ovo bilo dvije linije aka find & rename, covjek trazio bas bash, budem se naspavao pa odvalim ovo u perlu, mada i dalje bash je bash
u svakom slucaju predlozio bi da upucas tacni target ili odrilike path gdje se nalazi ti fajovi kao i tacno puno ime fajla kojeg mijenjas inace ne garantujem za nista iako se napravi backup od tih fajlova nista nije sigurno sto se u 15 minuta odradi, ali bi trebalo raditi