========================
advanced-bash-techniques
========================

.. raw:: html

   <table class="provenance-header" style="border: 0; border-collapse: collapse; margin: 0 0 16px 0; width: 100%;">

.. raw:: html

   <tr style="border: 0;">

.. raw:: html

   <td style="border: 0; vertical-align: top; padding: 0 24px 0 0;">

..

   | **Source:**
     https://foxhop.net/f348124b-2f95-11f1-a15f-e86a64d24d78/advanced-bash-techniques
   | **Snapshot:** 2026-10-16T04:42:34Z
   | **Generator:** Remarkbox ``d747782``

   *This is a thread snapshot. The living document lives at the source
   URI above — it may have been edited, extended, or replied-to since.*

.. raw:: html

   </td>

.. raw:: html

   <td style="border: 0; vertical-align: top; width: 200px; text-align: right;">

.. figure:: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKQAAACkAQAAAAAxzrjsAAABcklEQVR42tWXwW3DUAxDhS6g/bfkBiof7aKXXgrw0P4Eie0AtMxPUsrcD0vzh6/OzB4/6iTlVB/z0/rN1bm9W/G6BTOnFVxjybAueQDltIVrDozpoqeL62IN/vBQw/Wz78AEzF6N3xT5vWp6yLLSxkV/qa/Cg9iuMw+Sz9XCNaoGSKOzi+rtmxmN0kDmRh1+B/9aCKvHxyUeSAeo8DtFq1ZvJHuUjDWuxK/Qg2IJvOzaW/s2EQMa08RwHR4sLPIXtS1i6OWvyBp/KbGjVj4cObmJyhyUcC0ETDyUjZqnxW86XJLnsUcpH8wrG+cPgm1avhBGewWM9Vr5kKQkeRDxZutK9dIvdE/ZpE8rfxEXyZODKfoie5W2vNxnW3p4Q+dpQy0e0pEnAfxqudePF4ndhOMpzg9p7oxT6XHFeWf3dfNj59p8ljkSezwTRQs3j3+xs1r9IvUu5DLqMKv2+I2IR2n4as0PyZt5Ah2/qThP/pv/WZ9Agd+F/ynbvgAAAABJRU5ErkJggg==
   :alt: Scan for living source

   Scan for living source

.. raw:: html

   </td>

.. raw:: html

   </tr>

.. raw:: html

   </table>

advanced bash techniques
========================

find and change all files

.. container::
   :name: cb1

   ::

      sudo find . -type f -print0 | xargs -0 ls -hal

      sudo find . -type f -print0 | xargs -0 sudo chown www-data:www-data

      sudo find . -type f -print0 | xargs -0 sudo chmod 0644

find and change all directories:

   sudo find . -type d -print0 \| xargs -0 sudo chmod 0775

Remove a particular line from all files in a directory
======================================================

.. container::
   :name: cb2

   ::

      for file in /tmp/test/*
        do
          cp -p $file $file.tmp
          egrep -v "RegexToRemove" $file.tmp > $file
          rm -rf $file.tmp
        done

apache logs count unique ip addresses
=====================================

.. container::
   :name: cb3

   ::

      sudo cat access.log | awk '{ print $1; }' | sort | uniq -c | sort -n

Bash Perl search and replace multiple files one liner
=====================================================

.. container::
   :name: cb4

   ::

      perl -pi -w -e 's/search/replace/g;' *.extension

explanation:

   **-e**
      means execute the following line of code.

   **-i**
      means edit in-place

   **-w**
      write warnings

   **-p**
      loop

Bash sed search and replace multiple files one liner
====================================================

.. container::
   :name: cb5

   ::

      sed -i'' -e 's/search/replace/g' *

Get a coma separated list of libvirt guests
===========================================

.. container::
   :name: cb6

   ::

      virsh list | egrep -v 'Domain|Name|----|^$' | awk '{printf "%s," , $2}' > /tmp/virthosts

--------------

| **Source:**
  https://foxhop.net/f348124b-2f95-11f1-a15f-e86a64d24d78/advanced-bash-techniques
| **Snapshot:** 2026-10-16T04:42:34Z
| **Generator:** Remarkbox ``d747782``
