chris clarke
software development that works…or something
Find which process is listening on a port
October 17, 2007 on 6:20 pm | In Unix | No CommentsTo find which process is listening on port 22:
lsof -i:22
Find out which port a process is listening on e.g. httpd:
lsof -P -c httpd -a -i
Or by PID:
lsof -P -p 267 -a -i
lsof can also be used to find the files a process has open. To look for where httpd writes it’s log files:
lsof -c httpd | grep log
Unix stuff
October 17, 2007 on 5:58 pm | In Unix | No CommentsIf you work a lot on slow Solaris boxes, you can speed up finds a little bit:
find . -depth -type f -name '*.monkeys'
The depth option looks at the contents of directories before the directory itself and the ‘type f’ constricts the find to only look at regular files. You can speed it up a bit more if you have more information about the file e.g. the owner:
find . -depth -type f -user dev -name '*.monkeys'
Looking for something inside a file and only want to list the files containing your search:
grep -l monkeys *
Working on a box that doesn’t have recursive grep:
find -X . -depth -type f -name '*' | xargs grep -l monkeys
The ‘-X’ option ignores files that would mess with xargs e.g. filenames with spaces in.
GNUs Not U 4
March 5, 2007 on 10:44 pm | In Unix | No CommentsHere’s a puzzler. Minimize the following command which looks for multiple entries of a command in the PATH environment variable:
echo $PATH | awk 'BEGIN { RS=":" } ; { print; }' | xargs -I{} find {} -name ant
You could use which:
which ant
But that will only return one answer.
Could use locate:
locate ant
But that will return things not in the PATH.
Could use whereis:
whereis ant
But that only looks in a list of standard linux places, the PATH may be unconventional.
GNUs Not U 3
February 17, 2007 on 8:18 pm | In Unix | No CommentsStop a command from exiting after you logout:
nohup monkeys.sh
Convert html so it can be displayed on a web page:
cat index.html | perl -pe 's/</</g; s/>/>/g'
See the strings in a binary file:
strings /bin/rm
Show line numbers of a file:
nl monkeys.c
See a particular line in a file:
cat buildOutput.txt | nl | awk '$1==5 {print; }'
Concatenate files horizontally:
paste times dates people
View and save output at the same time:
ls | tee filesInthisDirectory.txt
Say yes to everything:
yes | rm -i *.txt
Send a message to everyone:
echo 'all your base are belong to us' | wall
See a list of processes every 5 seconds:
watch -n5 "ps aux"
Sort by the second column of a file:
cat file.txt | sort -k2
Delete a bunch of files matching a regex:
find . -name "*.svn" -exec rm -rf '{}' \; -print
Reverse a string:
echo "Never odd or even" | rev
GNUs Not U 2
January 28, 2007 on 1:16 am | In Unix | No CommentsKill a command by name:
ps axc | grep httpd | awk '{ print $1; }' | xargs kill
Look for a command exclude your own grep:
ps auxwww | grep command | grep -v grep
Kill a command based on it’s arguments:
ps axwww | grep port=8889 | grep -v grep | awk '{ print $1; }' | xargs kill
Can this be simplified?:
ps axc | awk '/httpd/{ print $1 }' | xargs kill
ps axwww | awk '/port=8889/{ print $1 }' | xargs kill
But when using the command above we are trying to kill our own awk command!
ps axwww | awk '/port=8889/ && !/awk/ { print $1 }' | xargs kill
GNUs Not U
January 24, 2007 on 7:56 pm | In Unix | No CommentsHighlight some expression using colour:
grep --color expression file
Go to a dir, execute a command and return to the current directory:
(cd dir && command)
A stopwatch:
time cat ^D
Download a url at 1 AM:
echo 'wget url' | at 01:00
Do some sums:
echo "monkeys=24; monkeys * 6 / 4" | bc
Convert a string to lowercase:
echo 'TeSt' | tr '[:upper:]' '[:lower:]'
List files that a process has open:
lsof -p 483
List processes that have the specified path open:
lsof ~
Display a webpage (MAC/BSD):
wget -qO - http://www.cheese.com > tmp.html && textutil -stdout -convert txt tmp.html
Look for a word case insensitively:
cat Names | grep -i demarco
Search and replace:
cat index.html | sed 's/Google/MSN/g'
Print lines 10 to 20:
cat index.html | sed -n '10,20p;20q'
Time how long it takes to calculate PI to 5000 decimal places:
time echo "scale=5000; 4*a(1)" | bc -l
Convert binary to decimal:
echo "obase=10; ibase=2; 01111111" | bc
Perform a calculation on some output:
ls -al *.html | awk '{sum+=$5} END{print sum}'
Fix typos in files but backup the original files:
perl -p -i.old -e 's/oldstring/newstring/g' *.txt
Execute a command on several computers:
perl -e 'for("B","C","D"){`scp command.pl $_:`; `ssh $_ command.pl`; }'
Look for a regular expression:
cat index.html | perl -ne 'print if /font-size:\s*\d+px/'
Print selected lines of a file:
cat buildOutput.txt | perl -ne 'print if 1..2'
Can you think of another way to print selected lines?
Comma-delimit:
ls -l | perl -wne 'BEGIN{$" = ","} @fields = split/\s+/; print "@fields\n";'
Find a file without the “permission denied” messages:
find . -name 'monkeys.txt' 2> /dev/null
Powered by Cheese.
RSS Entries Feed.
RSS Comments Feed
^Top^