Category Archives: Linux

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

Biggest folders in current path

If you need to find what is the biggest folders in current path in linux then this is the command:

du -sh * 2>/dev/null | sort -h

It needs couple of seconds to calculate (if you have slow io vps or hdd) and this is what you get:

root@vps3345:/home/nick/web# du -sh * 2>/dev/null | sort -h
713M    alle.ch
1.1G    moji.com
1.6G    rab.de
1.8G    zoek.nl
2.5G    al.at
5.4G    mojia.si
7.6G    a.co.uk
11G     sle.cz
13G     akceer.com

Linux screen

About linux screen program. Really nice to have it but usually when I find some article about using some linux software, there are always some missing pieces and then I have to search google again and again and that’s not good.

Type:

screen -> it opens up new screen window and you are in it

ctrl +a is for switching windows

ctrl+a c is for creating new screen window

Now to close it (detach) use:

ctrl+a d

To list all current screens:

screen -ls

To reattach to it use:

screen -r (number)

To close it use:

screen -XS (number) quit

Here are references:

https://linuxize.com/post/how-to-use-linux-screen/

https://askubuntu.com/questions/356006/kill-a-screen-session

Delete everything multiple files Linux

If you want to find and delete some files from your drive then use this command:

find . -type f ! \( -name "*thumb.jpg" -o -name "*1200w.jpg" \) -exec rm -rf {} \;

Explanation: https://unix.stackexchange.com/questions/78376/in-linux-how-to-delete-all-files-except-the-pattern-txt

https://unix.stackexchange.com/questions/50612/how-to-combine-2-name-conditions-in-find

Adding IP address – failover ip

First edit : nano /etc/network/interfaces

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
#allow-hotplug eno1
#iface eno1 inet dhcp
 auto eno1
 iface eno1 inet static
         address 195.154.242.173
         netmask 255.255.255.255
         gateway 195.154.242.1
 auto eno1:0
 iface eno1:0 inet static
 address 51.158.26.20
 netmask 255.255.255.255

And that’s all there is actually.

Use this as a reference.

https://documentation.online.net/en/dedicated-server/network/ip-failover/ip-failover

Rsync exclude

rsync -avz files/app_akcijekatalozi –exclude=storage/ akcijekatalozi@domain.net:/home/akcijekatalozi/

Take note, exclude path storage/ is relative to files/app_akcijekatalozi path…not root path, home path or absolute path!

Installing composer on KeyHelp control panel

I’ve been trying to figure this out for the last 2 hours. But I have finally managed to install it.

From offical composer page copy paste first two lines

https://getcomposer.org/download/

like:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'c31c1e292ad7be5f49291169c0ac8f683499edddcfd4e42232982d0fd193004208a58ff6f353fde0012d35fdd72bc394') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

Then, since KeyHelp makes users folders as root you will need to use i.e. files folder for installing Composer with command above. Then use this command:

COMPOSER_HOME=/home/users/someuser/files/ php composer-setup.php

What this command does?! COMPOSER_HOME is var and enables you to write composer.phar inside different home path.

All this without root. That was the point in the first place. Root and composer are not good option.

Problem was with the permissions in the first place. Users are from root. And Composer wanted to use $cd to access home folder. It poined to /home/users/someuser (someuser is current logged in user).

Error: Unable to create Composer home directory “/home/users/someuser/.config/composer”: mkdir(): Permission denied