4 # these great examples of handy unix tidbits were donated by "q. black".
7 # list a directory tree
8 ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
10 # list directory sizes (biggest first)
11 du -h $(du -s * |sort -nr|awk '{print $2}')
13 # this will sort a date field of the form: DD-MON-YYYY HH:MM:SS
14 sort +0.7 -1 +0.3M -0.6 +0 -0.2 +1
16 # this will sort a date field of the form: MON DD HH:MM:SS YYYY
19 # this will sort a date field of the form: MON DD HH:MM:SS
22 # this will sort a date field of the form: Date: Tue Feb 3 09:17:58 EST 2004
25 # display all lines from a certain line onward
27 |awk "{if (NR >= ${start_line}){print \$0}}"
29 # display all lines after a token
32 # print the first and last lines
35 # signal bash about a window size change
38 # show the date 1 year, 2 months and 3 days ago
39 date -v -1y -v -2m -v -3d
41 # set the date back 1 year
42 sudo date $(date -v -1y +%Y%m%d%H%M)
44 # output the standard date format for setting the time
46 date -u +%Y%m%d%H%M.%S
48 date -u (cut and paste from above)
50 # convert one date format to another (output is in the current time zone)
51 old_date="Aug 27 15:24:33 2005 GMT"
52 new_date=$(date -j -f "%b %e %T %Y %Z" "${old_date}" +%D)
56 # output the modification time of a file in different format
58 date -j -f "%b %e %T %Y" "$(ls -lT ${file} |awk '{print $6,$7,$8,$9}')"
60 # output the number of days until a certain date
61 target_date="Sep 2 15:20:20 2005 GMT"
62 target_seconds=$(date -j -f "%b %e %T %Y" +%s "${target_date}" 2>/dev/null)
63 diff_seconds=$(expr ${target_seconds} - $(date +%s))
64 diff_days=$(expr ${diff_seconds} / 86400)
65 echo "${diff_days} day(s)"
67 # these commands can be used to fill in missing times in a "uniq -c" count
69 # output 24 hours in one minute increments
70 for h in $(jot -w %02d - 0 23 1); do
71 for m in $(jot -w %02d - 0 59 1); do
75 # sort them together, and remove any 0 counts if an count already exists
76 sort +1 +0rn out1 out2 |uniq -f 1
78 # output with w3m to get basic html word wrap
79 w3m -T "text/html" -dump -cols 72 <<EOF
81 This test verifies basic networking and that the ${product_short_name}
82 can reach it's default gateway.
85 # another way to format text for output
87 This test verifies basic networking and that the ${product_short_name}
88 can reach it's default gateway.
94 asprintf(&jkwbuf, "JKW: msg->used = %ld\n", msg->used);
95 BIO_write(sc->log, jkwbuf, strlen(jkwbuf)+1);
99 # rolling diff of a list of files (diff a & b, then b & c,...)
101 for i in $(ls -1rt); do
102 if [ ! -z "${last}" ]; then
108 # clearing and restoring chflags
110 old_chflags=$(ls -lo ${file}|awk '{print $5}')
113 if [ ."${old_chflags}" != ."-" ]; then
114 chflags ${old_chflags} ${file}
117 # way to do standard edits to files
120 # append line(s) after a line, "i" to insert before
121 echo '/www_recovery/a'
122 echo 'mithril ALL = (root) NOPASSWD: /usr/local/libexec/destroyer'
125 echo 'g/^xntpd_program=/s,^xntpd_program=.*$,xntpd_program="ntpd",'
127 echo 'g/^controls key secret =/d'
131 # how to search for errors in the last 24 hours
132 # note that this command does not work quite right. The sort is off early
133 # in the year because the dates do not have the year.
134 # Also sed never sees the /CUT HERE/ when it is the first line.
135 (echo "$(date -v-24H "+%b %e %H:%M:%S") --CUT HERE--"; \
136 zgrep -h "cookie" /var/log/messages*)|sort +0M| \
138 # This version fixes those problems. It adds the file year to the date
139 # and puts a marker at the start of the list.
140 (echo "$(date -j -f "%s" 0 "+%Y %b %e %H:%M:%S") --ALWAYS FIRST--"; \
141 echo "$(date -v-24H "+%Y %b %e %H:%M:%S") --CUT HERE--"; \
142 for i in /var/log/messages*; do
143 year=$(ls -lT ${i}|awk '{print $9}')
144 zgrep -h "cookie" ${i}|while read line; do
145 echo "${year} ${line}"
147 done)|sort +0n +1M| sed '1,/CUT HERE/d'
149 # process a list of quoted values
151 # It tends to be easiest to use a 'here-document' to feed in the list.
152 # I prefer to have the list at the start instead of the end
154 'general' 'network node0' 'private address'
155 'general' 'options node2' 'kern securelevel'
157 }| while read line; do
163 echo "confutil value \"${config}\" \"${section}\" \"${key}\""
166 # Method to read lines with a "for" loop, without spawning a subshell
171 for line in $(cat /etc/passwd | sort -r); do
174 # do whatever you want here
175 echo "line = ${line}"
181 # generate a histogram of characters in a file
183 awk '{for (i=1; i <= length($0); i++) {printf("%s\n",substr($0,i,1))}}'|
186 # show line lengths for a file
187 cat file| awk '{print length($0)}'| sort -n
192 # get the modification time of a directory or file and then reset the time.
193 target=/usr/local/etc/pkdb
194 save_date=$(ls -ldT ${target}|awk '{print $6,$7,$8,$9}')
195 save_date=$(date -j -f "%b %e %T %Y" "${save_date}" +"%Y%m%d%H%M.%S")
197 touch -t ${save_date} ${target}
200 # detect NULL bytes in a file
202 hexdump -e '"%_u\n"' ${file}|grep -q '^nul$'
203 if [ $? -eq 0 ]; then
212 M<V-A;&4],PIT;W1A;#TP"F-O=6YT/3`*=VAI;&4@*#$I('L*("`@(&YU;2`]
213 M(')E860H*0H@("`@:68@*&YU;2`]/2`P*2!B<F5A:SL*("`@(&-O=6YT*RL*
214 M("`@('1O=&%L("L](&YU;0I]"B)T;W1A;"`]("([('1O=&%L"B)C;W5N="`]
215 K("([(&-O=6YT"B)A=F5R86=E(#T@(CL@=&]T86P@+R!C;W5N=`IQ=6ET"@``
218 (cat data; echo "0") |bc -q bc.average