Useful Scripts
iDog
Find & Replace Strings
- Search for string in all files in a dir and all of its sub-dirs
grep [-l] string $(find dir [-name filename])
[NOTE] $() is the same as ``
- String substitution in files
perl -pi~~~ -e 's/oldstr/newstr/' $(find dir [-name filename])
rm -rf $(find dir -name *~~~)
Redirection
File descriptors are represented as numbers. Following are those for std*:
file | file descriptor |
STDIN | 0 |
STDOUT | 1 |
STDERR | 2 |
- redirect stdout to file1, stderr to file2
app > file1 2> file2
- redirect both stdout and stderr to file
app > file 2>&1
- throw away output of some command (very useful in tasks in cron)
app > /dev/null 2>&1
- redirection both stdout and stderr to file, and show them in console
app 2>&1 | tee file
File Operation
Use command 'exec' to open and close files by file descriptors:
$ echo "Hello, world!" > aaa.txt # make a file aaa.txt by write something to it
$ exec 3 < aaa.txt # open file '3', input from aaa.txt
$ cat <&3 # redirect STDIN to '3'
Hello, world!
$ exec 3<&- # close file '3'
$ exec 4 > bbb.txt # open file '4' to output to bbb.txt
$ cat aaa.txt > &4 # redirect STDOUT to '4'
$ exec 4>&- # close file '4'
The file descriptor '3' is like a 'file handle' of file 'aaa.txt', opened for reading; while '4' is like a handle of file 'bbb.txt', opened for writing.
Network
tracert <ip_addr_or_host_name>
netstat -an
- web site performance test: use "ab" of Apache. Example: Simulate 1000 requests; 20 concurrent requests.
ab -n 1000 -c 20 http://idogicat.dip.jp/index.html
ab -n 1000 -c 20 http://idogicat.dip.jp/cgi-bin/webcat/bin/view
- resolve Apache log: use "logresolve" of Apache.
Add Color to Bash
To make ls show file list with colors:
ls --color
to configure the color:
dircolors –p > colors
# edit file colors to customize it to one's interest...
dircolors colors > .colors
# .colors is a script for the current shell. you can also modify on it to make it support other shells
source .colors # put in .bashrc
Running command periodically
This can be used to monitor something:
Watch –n <seconds> <command>
Eg:
Watch –n 5 uptime
Watch –n 3 ls –l bigLogFile.log
Merging files
Merge two files, only keep one copy of duplicated lines:
cat file1 file2 | sort | uniq
Only get lines existing in both files:
cat file1 file2 | sort | uniq -d
Only get lines existing in only one of two files:
cat file1 file2 | sort | uniq -u
[NOTE]: uniq command only works properly when the input is sorted.
redirect man/info to a file
man xxx | col -b > man.txt
info xxx -o info.txt -s
Extract files
unzip example.zip
tar xvf example.tar
tar xvfz example.tar.gz
tar xvfz example.tgz
tar xvfj example.tar.bz2
Linux 'tree' command
alias tree='find . -print | sed -e "s;[^/]*/;|__;g;s;__|; |;g" '
# iDog's version
alias tree='find . -print | sed -e "s;[^/]*/;+-- ;g;s;+-- +;| +;g;s;+-- +;| +;g;s;+-- |;| |;g"'