Good linux commands …

Here is a bunch of command that are really useful or just powerful .

Loving it.

1. Count Iteration of IP addresses

We could also count the unique ip addreses, like this:

cat yourlog|cut -d\  -f 1 -|sort|uniq -c

or with IP parsing


cat yourlog | grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" | sort -n | uniq -c | sort -n

2. Play with history

May look like a keyboard explosion, but this will search through your history and list the commands you use most often. This isn’t something you’d do regularly but now you see it in print it’s one you’ll probably be keen to try out just for interest’s sake.

history | awk ‘{a[$2]++}END{for(i in a){print a[i] “ “ i}}’ | sort –rn | head

awk -F':' '{ print $1 }' /etc/passwd

Check command history, but avoid running it.

!whatever will search your command history and execute the first command that matches ‘whatever’. If you don’t feel safe doing this put :p on the end to print without executing. Recommended when running as superuser.

!whatever:p

3. Sort the user list

awk -F':' '{ print $1 }' /etc/passwd

4. Remove empty lines

sed '/^$/d'

5. Twitter from shell.

curl -u user:pass -d status="Tweeting from the shell" http://twitter.com/statuses/update.xml

6. Find and Replace with SED

Imagine you have a large file ( txt, php, html, anything ) and you want to replace all the words “ugly” with “beautiful” because you just met your old friend Sue again and she/he is coming over for a visit.

$ sed -i 's/ugly/beautiful/g' /home/bruno/old-friends/sue.txt

7. Playing with time

Print yesterday’s date (today – 1) in the Year-Month-Day format

date --date "-1 days" +"%Y-%m-%d" will produce a result of 2010-11-10

Print last month’s date (today – 1 month) in the Year-Month-Day format

date --date "-1 month" +"%Y-%m-%d" will produce a result of 2010-10-11
date --date "yesterday" +"%Y-%m-%d"

It also understands “now” and “today”.

More examples …

8. Find and Replace a string into multiple files

The following script includes sub-directories and encapsulates the functionality in a file.


#!/bin/sh

if [ $# -lt 4 ] ; then
echo
echo Wrong number of parameters.
echo Try again using the following format:
echo “myreplace ‘filepattern’ findstring replacestring” “seperationstring”
echo
echo example:
echo “myreplace ‘*.txt’ \”My dog tries to eat the neighbour’s cat.\” \”My dog tries to eat the neighbour’s cat food.\” ‘#’”
echo
exit 1
fi

echo All instances of :
echo $2
echo
echo will be changed to:
echo $3
echo
echo in the following files:
find . -name “$1″ -exec grep -l “$2″ {} \;
echo

IFS=$’\n’

for i in $(find . -name “$1″ -exec grep -l “$2″ {} \;)
do
echo “working on file ” “$i”
# uncomment when you confirm that it works
#cp “$i” “$i.myreplace_saved”
#sed “s$4$2$4$3$4g” “$i.myreplace_saved” > “$i”

# rm $i.myreplace_saved # coment this line to save backup.
done

echo
echo All changes have been made.
echo

###################################

9. Search the last modified files (Linux)

Here is a smart command to find the last modified files in your Linux / UNIX system. You have to exclude /dev /sys et /proc and you add the time depth you want to check (here 60 min).

find / -not -path '/sys*' -not -path '/dev*' -not -path '/proc*' -mmin -60

Be the first to comment

Leave a Reply

Your email address will not be published.


*


*