Differences between revisions 6 and 7
Revision 6 as of 2020-06-26 13:19:18
Size: 1573
Editor: PieterSmit
Comment:
Revision 7 as of 2024-01-24 20:48:46
Size: 2068
Editor: PieterSmit
Comment:
Deletions are marked like this. Additions are marked like this.
Line 13: Line 13:

== Delete files older than 30 days ==
 * Check 1st {{[
find /opt/backup -type f -mtime +30
}}}
 * Permanently delete ALL the files in folder older thatn 30d {{{
find /opt/backup -type f -mtime +30 -delete
}}}
 * Permanently delte ALL files with a specific extention older than 30d {{{
find /var/log -name "*.log" -type f -mtime +30 -delete
}}}
 * Note when deleting dir's with {{{ -type d }}} be carefull parent dir might have old modification and everything below will be lost.

Describe Linux/FindGrepAwkSedXargs here.

Find files containing something in Linux

Search dir for files containing string and replace all of them

##e.g. search for tentant -> tenant
grep -irlZ 'tentant' ./ |  xargs -0  sed -i 's/tentant/tenant/g'

Delete files older than 30 days

  • Check 1st {{[

find /opt/backup -type f -mtime +30 }}}

  • Permanently delete ALL the files in folder older thatn 30d

    find /opt/backup -type f -mtime +30 -delete 
  • Permanently delte ALL files with a specific extention older than 30d

    find /var/log -name "*.log" -type f -mtime +30 -delete 
  • Note when deleting dir's with  -type d  be carefull parent dir might have old modification and everything below will be lost.

Bash loop over files

  • Rather than using find and xargs, use globing to find and process files in bash
    • e.g.

      #By enabling the globstar option, you can glob all matching files in this directory and all subdirectories:
      # Make sure globstar is enabled
      shopt -s globstar
      for i in **/*.txt; do # Whitespace-safe and recursive
          process "$i"
      done

awk script to duplicate section under " labels:" of k8s yaml file to " spec: { selector: { matchLables: <...> } }

  • Explain
    1. looks for /Deployment/ set f=0
    2. look for /labels:/ but only if f=0, set it to 1
    3. look for /spec:/ only if f=1, increase it and run only if=2, now insert info captured in var a
    4. else save line in a[c++] if f
  • Script

    # Reads k8s deployment.yaml, copy the labels: to spec: selector:
    gawk -i inplace '
    { print }
    /Deployment/ { f=0 }
    /labels:/ { if(f == 0) f=1 }
    /spec:/ { if(f == 1) {
        f=f+1
        if (f==2) {
          print "  selector:"
          print "    matchLabels:"
          for(i=2;i<=c;i++) print "  "a[i]
        }
      }
    }
    f{ a[++c]=$0 }
    ' $1


CategoryLinux

Linux/FindGrepAwkSedXargs (last edited 2024-01-24 20:48:46 by PieterSmit)