Category Archives: Programming

About programming in general

Change character in filename linux bash

I’m figuring this out for some time. It’s not that I can’t read but most of the time when trying out bash stuff you can make more damage then good to your system so you have to be really really easy.

Now, this one here makes the difference. I had a job to change & in many many many files in different directories and subdirectories and after trying many scripts this one came up working just fine and fast.

find . -type f -exec rename 's/string1/string2/g' {} +

If you want to target specific filename extension then you need to add:

find . -type f -name "*.webp" -exec rename 's/&/-/g' {} + 

It was taken from here:

https://unix.stackexchange.com/questions/416018/how-to-replace-a-string-in-all-folder-and-file-names

Bash script for transferring files

url="187.2.2.2"
domain="somedomain.com"
db_user="nikola_db2"
db_database="nikola_db2"
cd /home/nikola/web/$domain/public_html/
rm -rf $db_database*
mysqldump --skip-tz-utc -u$db_user -psomething  $db_database > $db_database.sql --net-buffer-length  16777216
ssh -p 4444 nikola@$url 'cd /home/nikola/web/'"$domain"'/public_html/; rm -rf '"$db_database"'*;'
rsync -avz -e "ssh -p 4444" /home/nikola/web/$domain/public_html/$db_database.sql nikola@$url:/home/nikola/web/$domain/public_html/
ssh -p 4444 nikola@$url 'cd /home/nikola/web/'"$domain"'/public_html/; mysql -'"$db_user"' -psomething '"$db_database"' < '"$db_database"'.sql'
rsync -avz -e "ssh -p 4444" /home/nikola/web/$domain/public_html/ nikola@$url:/home/nikola/web/$domain/public_html/
#rsync -avz -e "ssh -p 4444" /home/users/nikola/www/somedomain.com/ nikola@$url:/home/users/nikola/www/somedomain.com/

PHP Mysql extending class

So, lets say you want to extend php/mysql class we were talking about earlier.

class nastavak extends baza {

private $nast;
private $drugi;

public function __construct(){
echo "This is the extended class";
parent::__construct("","root","","test");
parent::sqlquery("select * from imena");
parent::stampaj();
}
}

What happened here? Well, we have added another class on top of baza class and that way we have extended it. It means new class (extended) has everything from parent class (baza) and what we have added inside of it (nastavak).

Overriding in the next article.

PHP Mysql class for websites

I had some free time and decided to write one simple php class for manipulating mysql. It’s really simple code, it can help new programmers learn how to build their own classes.

<?php

class baza {

 private $username;
 private $password;
 private $local;
 private $errors;
 private $resource1;
 private $db_sel;
 private $sqlupit;

 public function __construct($l="localhost",$u, $p, $db_selected){
 $this->username=$u;
 $this->password=$p;
 $this->local=$l;
 $this->db_sel=$db_selected;
 $this->conn();
 $this->db_selected();
 $this->listaj_db();
 }

 private function conn(){
 $this->resource1=mysql_connect($this->local,$this->username,$this->password);
 if (!$this->resource1){
 echo "Postoji problem sa konekcijom <br />";
 }
 else {
 echo "Sve je super <br />";
 }
 }

 private function db_selected(){
 mysql_select_db($this->db_sel);
 }

 private function listaj_db(){
 $result = mysql_query("SHOW DATABASES"); 
 while ($row = mysql_fetch_array($result)) { 
 echo $row[0]."<br>"; 
 }
 
 }
 
 public function sqlquery($n){
 $this->sqlupit=mysql_query($n);
 }
 
 public function stampaj(){
 //$result = mysql_query("SHOW DATABASES"); 
 while ($row = mysql_fetch_array($this->sqlupit)) { 
 echo $row[1]."<br>"; 
 }

 }
}

$n=new baza("","root","","test");
$n->sqlquery("select * from imena");
$n->stampaj();

?>

Please comment if you have some questions.