Find zip files and unzip into directory based on zip file name in linux

This command is a neat way to automate extracting multiple zip files into directories named after the files themselves. It avoids the need to unzip files manually one by one, which is particularly useful in large directories or complex file structures.

find . -iname '*.zip' -exec sh -c 'unzip "{}" -d "${1%.*}"' _ {} \;

Explanation:

  1. find .
    • The find command searches through directories recursively, starting from the current directory (.). It looks for files that match specific criteria.
  2. -iname '*.zip'
    • This option tells find to look for files with names ending in .zip. The -iname flag makes the search case-insensitive, meaning it will match .zip, .ZIP, or any other case combination.
  3. -exec
    • The -exec option allows you to run a command on each file that find locates. In this case, it runs a shell script to unzip the files.
  4. sh -c 'unzip "{}" -d "${1%.*}"' _ {}
    • sh -c runs the specified shell command. Here’s what this part does:
      • 'unzip "{}" -d "${1%.*}"': This is the actual unzipping command. unzip extracts the contents of the zip file ("{}" refers to the zip file found by find).
      • -d "${1%.*}": This specifies the target directory where the file should be extracted. The syntax ${1%.*} removes the .zip extension from the filename to use the base name of the zip file as the folder name. For example, if the zip file is example.zip, it will create a folder example to extract the contents into.
      • The first underscore (_) is a placeholder used by sh -c to refer to the next argument ({}), which is the zip file path that find passes to the command.
  5. {} \;
    • {} represents each file found by find, and \; terminates the -exec command.

Example:

Suppose you have the following structure:

/home/user
├── folder1
│ └── file1.zip
├── folder2
│ └── file2.ZIP

Running the command will:

  • Find file1.zip and file2.ZIP.
  • Extract the contents of file1.zip into a folder named file1 and file2.ZIP into a folder named file2.

Bulk change PHP memory_limit across all installed PHP versions

On my server, I have PHP 5.6 to 8.1 installed and use the versions for various testing purposes.

To change the memory_limit across all versions as simply as possible instead of editing each file, I use the following command.

find /etc/php -iname 'php.ini' -exec sed -i 's/memory_limit = .*/memory_limit = 512M/g' {} \;

ISPConfig3 – DNSSEC ERROR: We are low on entropy.

It seems that ISPConfig3 checks for entropy availability to be below 200 and also 400 per the following file, /usr/local/ispconfig/server/bind_plugin.inc.php.

Line 93 and line 210 check for entropy availability.

Line 93, inside function soa_dnssec_create():

if (file_get_contents('/proc/sys/kernel/random/entropy_avail') < 400) {
	$app->log('DNSSEC ERROR: We are low on entropy. Not generating new Keys for '.$domain.'. Please consider installing package haveged.', LOGLEVEL_WARN);
	echo "DNSSEC ERROR: We are low on entropy. Not generating new Keys for $domain. Please consider installing package haveged.\n";
	return false;
}

Line 210, inside function soa_dnssec_update():

                if (file_get_contents('/proc/sys/kernel/random/entropy_avail') < 200) {
                        $app->log('DNSSEC ERROR: We are low on entropy. This could cause server script to fail. Please consider installing package haveged.', LOGLEVEL_ERROR);
                        echo "DNSSEC ERROR: We are low on entropy. This could cause server script to fail. Please consider installing package haveged.\n";
                        return false;
                }

My problem seems to be entropy_avail is 256.

Researching this, I found this Unix StackExchange article, kernel 5.10.119 caused the values of /proc/sys/kernel/random/entropy_avail and poolsize to be 256 – Unix & Linux Stack Exchange, that describes a recent change in the Linux Kernel 5.10.119.

I am currently on Linux kernel 5.10.127-1 (2022-06-30).

To work-around this, I adjusted the checks to both be 200, instead of one being 200 and the other 400 (on creation of DNSSEC records).

I was able to successfully generate the DNSSEC for my zone and issue /usr/local/ispconfig/server/server.sh without additional error.

This is probably NOT the best way to handle this… but I’m not sure what else to do at this point.

sed replace braces with brackets keeping content inside

Took me a minute to figure this out but it works.

Given the following string, I want to replace { and } with [ and ] keeping each number inside and only with a number inside the braces without space and without letters.

data.txt:

{1}: Today is tomorrow's yesterday.
{2}: This year is next year's yesteryear.
{3}: Foo
{10}: Bar
{100}: Baz
{91919}: Qux
{99119a9}: 42
sed -e 's/{\([0-9]\+\)}/[\1]/ data.txt
rjk@debian:~$ sed -e 's/{\([0-9]\+\)}/[\1]/' data.txt
[1]: Today is tomorrow's yesterday.
[2]: This year is next year's yesteryear.
[3]: Foo
[10]: Bar
[100]: Baz
[91919]: Qux
{99119a9}: 42
{1 2 3 4 5}: infinity and beyond.

If I want to replace anything in the braces, then I could alter the command slightly.

sed -e 's/{\(.*\)}/[\1]/ data.txt
rjk@debian:~$ sed -e 's/{\(.*\)}/[\1]/' data.txt
[1]: Today is tomorrow's yesterday.
[2]: This year is next year's yesteryear.
[3]: Foo
[10]: Bar
[100]: Baz
[91919]: Qux
[99119a9]: 42
[1 2 3 4 5]: infinity and beyond.

How to remove a systemd service

I’m not sure why systemd doesn’t remove the service, but to do so you can run through the following commands. Also check /etc/init.d/[servicename] as there may be a wrapper there as well.

If you know what service you’re looking to remove, great. If not, you can quickly find all the systemd services enabled on your system with the following command:

systemctl list-unit-files | grep enabled

You can inspect the service and find any unit information for it using the following:

systemctl cat [servicename]

To continue on, stop, disable and remove the unit links as shown below.

systemctl stop [servicename]
systemctl disable [servicename]
rm /etc/systemd/system/[servicename]
rm /etc/systemd/system/[servicename] # and symlinks that might be related
rm /usr/lib/systemd/system/[servicename]
rm /usr/lib/systemd/system/[servicename] # and symlinks that might be related
systemctl daemon-reload
systemctl reset-failed