Saturday, December 27, 2014

List file type counts in a directory based on file ext

   ### print counts of all file types in a directory based on file extension
alias lsx='ls -f1 | sed "s/\(.*\)\.\(.*\)/\2/" | sort | uniq -c | sort -n'

Wednesday, December 24, 2014

Figure out what command your running w/: command -V

$ command -V echo
echo is a shell builtin
$ command -V /bin/echo
/bin/echo is /bin/echo
$ command -V ls
ls is aliased to `ls --color=auto'

Fastest Way to Count Files in Dir: ls -f1 | wc -l

$ time set -- *; echo $#         $ time ls | wc -l
real    0m0.715s                    25878                  
user    0m0.678s                  real   0m0.198s 
sys    0m0.010s                   user   0m0.159s
25870                                  sys    0m0.013s

$ time ls -1f | wc -l 
25878                   
real    0m0.045s   
user    0m0.016s  
sys    0m0.007s    

This option is the clear winner, without sorting -f and the -1 for 1 entry per line is what most *nix's will have ls provide to a pipe yet it is better to be explicit.  

Tuesday, December 23, 2014

Bash Dynamic Brace Expansion w/Eval & w/o * files -- shopt -s nullglob

If you don't turn the Bash shell option nullglob on you will get *'s for the extensions you want to process.
$BRACES="{jp3g,jp4eg,g2if}"  
$ for a in $(eval "echo *${BRACES}"); do echo $a; done
*jp3g *jp4eg *g2if
$ shopt -s nullglob
$ for a in $(eval "echo *${BRACES}"); do echo $a; done
$
Voila, no more expansion to non-existent file names!

Monday, December 22, 2014

Simple Bash Function/Script to Convert Numbers to Strings e.g. 45 => Forty Five

Begin num2str.bsh :
(Thanks to logic I found somewhere on the internet in a file entitled NumberWordConverter.java - I can't recall exactly where I found it since I have since had problem with my browser and wiped the history out - I only converted it to Bash)   

#!/usr/bin/env bash

declare -a units=("" "one" "two" "three" "four" "five" "six" "seven" \
            "eight" "nine" "ten" "eleven" "twelve" "thirteen" "fourteen" \
            "fifteen" "sixteen" "seventeen" "eighteen" "nineteen")
declare -a tens=("" "" "twenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty" "ninety")

function convert() {
declare -i INTEGER=$1
 if (($INTEGER < 0)); then
   echo -en "minus "
   convert $(($INTEGER*-1))
elif (($INTEGER < 20)); then
   echo -en ${units[$INTEGER]}
elif (($INTEGER < 100)); then
   echo -en ${tens[$(($INTEGER/10))]} 
   (($INTEGER%10)) && echo -en " "
   echo -en ${units[$(($INTEGER%10))]}
elif (($INTEGER < 1000)); then
   echo -en "${units[$(($INTEGER/100))]} hundred"
   (($INTEGER%100)) && echo -en " "
   convert $(($INTEGER%100))
elif (($INTEGER < 1000000)); then
   convert $(($INTEGER/1000))
   echo -en " thousand"
   (($INTEGER%1000)) && echo -en " "
   convert $(($INTEGER%1000))
elif (($INTEGER < 1000000000)); then
   convert $(($INTEGER/1000000))
   echo -en " million"
   (($INTEGER%1000)) && echo -en " "
   convert $(($INTEGER%1000000))
else
   convert $(($INTEGER/1000000000))
   echo -en " billion"
   (($INTEGER%1000000000)) && echo -en " "
   convert $(($INTEGER%1000000000))
fi
}
convert $1
echo 


$ num2str.bsh 4541
four thousand five hundred forty one
$ num2str.bsh 45414
forty five thousand four hundred fourteen
$ num2str.bsh 4541443
four million five hundred forty one thousand four hundred forty three

Sunday, December 21, 2014

Quick Anagramatic Glob * Exception e.g. *[^host]*

/var$ ls
backups  cache  host  lib  local  lock  log  mail  opt  run  spool  tmp

/var$ ls -d *[^osht]*
backups  cache  lib  local  lock  log  mail  opt  run  spool  tmp

Of course this approach fails if you have an anagram so it should never be used for coding something serious. Yet its useful enough when you want to narrow something down when playing around or if you know for sure that you don't have anagrams to worry about.

e.g. ls -d /tmp/*[^.txt]  will list all the files without the .txt extension
e.g. ls -d /tmp/*[^.xtt]  does the same.. so be careful =)

Also if you don't mind playing with shell options you can more simply set the extglob option, yet I don't really recommend shoptions much since you can easily forget that your env is different and they tend to make things unpredictable and less portable (e.g. leaning tower of babel).

/var$ shopt -s extglob
/var$ ls -d !(host)
backups  cache  lib  local  lock  log  mail  opt  run  spool  tmp

Sunday, December 14, 2014

Bash Unset Readonly Variable - Hack

Found on the web at: 
(http://stackoverflow.com/questions/17397069/unset-readonly-variable-in-bash)  
E.g. Accidentally a READONLY variable is declared at Bash shell prompt
Here is the test script commands:   
declare -r RESTORE_BASHNOCASEMATCH="$(shopt -p nocasematch)"
echo "$RESTORE_BASHNOCASEMATCH"
unset RESTORE_BASHNOCASEMATCH
cat << EOF | sudo gdb -silent
attach $$
call unbind_variable("RESTORE_BASHNOCASEMATCH")
detach
EOF
echo "$RESTORE_BASHNOCASEMATCH"
unset RESTORE_BASHNOCASEMATCH 
Here is the result:   
$ declare -r RESTORE_BASHNOCASEMATCH="$(shopt -p nocasematch)"
$ echo "$RESTORE_BASHNOCASEMATCH"
shopt -u nocasematch
$ unset RESTORE_BASHNOCASEMATCH
bash: unset: RESTORE_BASHNOCASEMATCH: cannot unset: readonly variable
$ cat << EOF | sudo gdb -silent
> attach $$
> call unbind_variable("RESTORE_BASHNOCASEMATCH")
> detach
> EOF
(gdb) Attaching to process 5207
Reading symbols from /bin/bash...(no debugging symbols found)...done.
Reading symbols from /lib/x86_64-linux-gnu/libtinfo.so.5...(no debugging symbols found)...done.
 .... 
(gdb) Detaching from program: /bin/bash, process 5207
(gdb) quit 
$ echo "$RESTORE_BASHNOCASEMATCH"
$ unset RESTORE_BASHNOCASEMATCH
$  
No more errors, it was a success! 

Saturday, December 13, 2014

Emulate banner via figlet

alias mybanner='_(){ [[ $# -lt 3 ]] && echo "Usage: mybanner <chr> <string>"; TRCHR=$1; shift; figlet -f banner "$*" | tr "#" "$TRCHR"; }; _'

(figlet - Frank, Ian & Glenn's Letters - figlet can be installed via apt-get install figlet)

Friday, December 12, 2014

Ways to Replace Multiple Spaces in Text (Awk,Sed,tr)

The awk way:  
$ cat file.txt | awk '{gsub("[ ]+"," ");print}' 
The tr way:
$ cat file.txt | tr -s " " 
where -s stands for squeeze and can be used on any character not only spaces
The sed way: 
$ cat file | sed -r 's/[ ]+/ /g'

Thursday, December 11, 2014

Skip Variables in Awk (like shift in Bash except nonsequential)

All you need to do is set the variable to null and they disappear!

$ echo a b c d e f g | awk '{$1="";$3="";print}' 
b  d e f g

Wednesday, December 10, 2014

Using Kill To Signal a Process By Name / Taming CRAS

Sometimes CRAS (Chromium [OS] Audio Server) runs up the CPU to 100% sometimes even over 100% - if you send the hangup signal, that should save you trouble of needing to reboot:

$ kill -SIGHUP $(read a _< <(ps -eo pid,comm | grep -i cras); echo $a)

(similar to killall -r '.*[cC][Rr][aA][sS].*' -s SIGHUP )

or you can kill bash shell script sub processes like this: 

$ kill -SIGHUP $(ps -ejH | awk '!/bash/{ if ($2 ~ <process gid>) print $1}')

Tuesday, December 2, 2014

Rename directories that have special characters

while read a; do TMP="$a"; for b in {1..50}; do TMP=$(echo "$TMP" | sed "s/[^0-9A-Za-z]/$(($RANDOM%10))/"); done; mv "$a" "$TMP"; done < <(find . ! -path . -type d -printf "%f\n")