Mittwoch, 3. August 2011

Starbuck's Unix Tricks

This is just a collection of small tricks I found useful when working on a Unix shell

Rename multiple files
Following bash snippet solves a very common task. You have a list of files or folders and want to add a prefix or suffix. Please note that this little for loop do not differentiate folders and files. Found it nevertheless quite useful:

for f in `ls`;do mv "$f" "$(echo $f"_mysuffix")"; done

Recursive grep on Solaris
On Solaris you usually don't have a recursive grep so you need to work with find and grep.
find . -name *.conf | xargs grep -i 'PATTERN'

Creating folder structures
Following snippet shows how to create based on a list of base folder how to create a common deep folder structure. Things to notice here is -p switch for mkdir and how to iterate over comma separated lists.
#!/bin/bash

IFS=","
FOLDERS="a,b,c"
#cd /yourbasefolder
for i in $FOLDERS
do
   mkdir -p $i/your/folder/structure
done


Count frequencies
Below snippets extracts from an access logfile (apache, application server, etc) column with requester's IP address and compute frequencies. It is piping logfile content to awk to extract on column (separator configured with FS). Next pipe sorts IP addresses, couting step is then done with uniq another nice UNIX tool. Last not least couting is sorted to have a nice chartlist.

cat logs/access.log | awk '{FS = "\t"}; {print $5}' | sort | uniq -c | sort

Keine Kommentare:

Kommentar veröffentlichen