KitzKikz  KitzKikz: ShellTricks   RecentChanges 
 PopularPages 
 SearchPages 
 Home | Trail - ShellTricks
 

  These are some unix shell (bash/ksh/tcsh/...) tricks I've collected. I'm placing them here because, typically I'm away from my personal machine when I most need one.

----

__Last Workday/Weekday of the Month__ (see ["here" http://forums.macosxhints.com/showpost.php?s=a67d9453f9c5581c62107fa4c33ef19b&p=505614&postcount=17])
<code>
cal | awk '{print $2,$3,$4,$5,$6}' | tr -s '[:blank:]' '\n' | tail -1
</code>

----
__Yesterday in Perl__
<code>
perl -e '@T=localtime(time-86400);printf("%2d/%2d/%4d\n",$T[4]+1,$T[3],$T[5]+1900)'
perl -e 'use POSIX;print POSIX::strftime("%m/%d/%Y\n",localtime(time-86400))'
</code>

----
__Fast Process Search (fps)__ - I'm constantly checking to see if a certain program or process is running. This helps reduce the typing and the clutter.
<code>
function fps {
(( $# )) &&
ps -aux | sed -n "1p;/___fps___/d;/$1/p" ||
echo "Usage: fps <search string>"
}
</code>

Modified for use with multiple words in any order:

<code>
function fps {
typeset x y
for x in $@; do y="$y/$x/!d;"; done
ps -ef | sed -n "1p;/___fps___/d;${y}p"
}
</code>


----
__List All Available Man Pages__:
<code>
man --path | perl -pe 's/:/\n/g' | xargs -J% -n1 find % -type f | xargs basename | sort -u
</code>


----
__Dos2Unix Line Ending Conversion__:
<code>
perl -pi -e 's/\r\n/\n/;' filename ...
</code>


----
__Subtract Files:__ Output all lines in FileA not also in FileB
<code>
sort FileA | uniq | sort - FileB FileB | uniq -u
</code>
| FileA | FileB | Result |
|{ valign=top } a%%%a%%%b%%%c%%%d%%%e | c%%%d%%%e%%%f%%%g | a%%%b |

If you know that FileA contains unique lines only, then this simplifies to:
<code>
sort FileA FileB FileB | uniq -u
</code>


----
__Programmatically Edit Crontab__

<code>
# Disable all cron jobs
EDITOR='perl -pi -e s/^/#off#/' crontab -e; crontab -l

# Enable all cron jobs
EDITOR='perl -pi -e s/^#off#//' crontab -e; crontab -l
</code>

----
__Find the location of a Perl Module__
<code>
perl -MXML::Parser -le 'print join qq(\n), grep /Parser/, values %INC'
</code>

----
__Preserve File Modification Time__
<code>
x=filename

/usr/bin/perl -e '
my ($ss,$mm,$hh,$DD,$MM,$YY)=localtime((stat($ARGV[0]))[9]);
printf "%04d%02d%02d%02d%02d.%02d",$YY+1900,$MM+1,$DD,$hh,$mm,$ss,$ARGV[0];
' "$x" | read t

Modify the file

/bin/touch -t $t "$x"
</code>
Entirely within perl:
<code>
$filename = $ARGV[0];
($atime,$mtime) = (stat($filename))[8,9];

open(FP,">>$filename");
print FP "Add a line of text.\n";
close(FP);

utime($atime,$mtime,$filename);
</code>
 

 
  ·  0.1196s