Tag: bash
Facebook auto poker
by z3n on Dec.27, 2011, under Linux Happyness, Tips & Hints
I wrote this macro with XTE intended to run on linux on a VNC screen, nothing a regular geek wouldn’t understand.
Never loose a poke war ever again:
while [ 1 ] ; do xte 'mousemove 20 350' 'mouseclick 1';sleep 5;xte 'mousemove 300 235' 'mouseclick 1';sleep 30;done;
Finding big files on linux
by z3n on Apr.02, 2011, under Linux Happyness
Problem:
How to find big files on linux?
Solution:
find / -type f -size +50000k
This will find files bigger than 50MB (50000k)
rsync over ssh with custom port
by z3n on Oct.29, 2010, under Linux Happyness
Problem:
How to do a rsync with custom ssh port ?
Solution:
This was a bit hard to find, so i’m posting it.
add:
-e “ssh -p PORT_NUMBER -C -oCompressionLevel=9″
to the command, like this:
rsync -azv --bwlimit=400 -e "ssh -p 12345 -C -oCompressionLevel=9" YOUR_HAPPY_USER@YOUR_SERVER:/path/to/files/from/server /path/to/local
Source:
How to scroll on a bash screen
by z3n on May.25, 2010, under Notes, Tips & Hints
Problem:
It’s so cool to have many screens at bash, but i never figured out how to scroll up on them, somehow i never looked after as well.
Solution:
CTRL + A -> ESC -> PAGE UP / PAGE DOWN
Source:
(just) Find in files (bash)
by z3n on Dec.09, 2009, under Linux Happyness, Tips & Hints
Problem:
Need to search for a string in many files and return the filenames.
Solution:
find -depth -name *.file_extension | xargs grep -sl “string“
Search in files and count lines
by z3n on Oct.27, 2009, under Linux Happyness
Problem:
I wanted to know how many occurences of a string appears on a log file, by ssh.
Solution:
cat file.ext | grep search_string | wc -l
cat will print the whole file contents, grep will search for the string, wc -l will count the lines.
Search in files and replace by bash
by z3n on Oct.26, 2009, under Coding, Linux Happyness, Tips & Hints
Problem:
How to change a string on several files by bash without opening one by one.
Solution:
for i in $(find . -type f); do sed ’s/find/replace/g’ $i > $i-tmp; mv $i $i-backup; mv $i-tmp $i; done
Yes, it’s just a copy paste from the source, i don’t have time to explain each command.
Source:
Restarting dead services using cron/bash
by z3n on Jun.11, 2009, under Coding, Linux Happyness
Problem:
Some services keep dieing and there’s nothing much you can do about it, since they don’t restart automatically.
Those services could be httpd/apache, mysqld, sshd, etc
Solution:
This little script will do the job:
pstree | grep httpd | wc -l | awk ‘{if ($1 == 0) system(“service httpd restart”) }’
in this example i’m checking if httpd (apache) is running.
broken down, pstree will list all your running processes,
grep httpd will search for “httpd” string on pstree,
wc -l will count the words from grep httpd, basically will return 0 or 1 if something was found,
and finally awk will check if wc -l is = 0 , meaning that the service / process in question is not running, then will perform the required action, on my example, service httpd restart which will restart apache.
You may also check it by port level as suggested by HTNet :
netstat -ln | grep “:80″ | wc -l | awk ‘{if ($1 == 0) system(“service httpd restart”) }’
You could build up a little .sh file and put it on crontab to check it every minute or whatever you like.
Mass moving files from a folder to another by bash
by z3n on Jun.05, 2009, under Linux Happyness
Problem:
How to move files from multiple subfolders using bash?
Solution:
for b in `find /path/to/search/ -depth -name filename_or_wildcard`;do mv $b /path/to/destination/${b##*/};done;
Example:
for b in `find ./ -depth -name *.tah`;do mv $b ../../TAH/${b##*/};done;
This will move all .tah files to ../../TAH/ no matter how depth on the search path they are they will be put all into the same folder, the main complication here is to get the basename of the filename, when you do a find it will show the whole path as the result, so this is where ${b##*/} come in, it will strip out the path and leave on the basename.
Source:
Sending a command result to another program
by z3n on Apr.21, 2009, under Linux Happyness, Tips & Hints
Problem:
I wanted to mass edit .htaccess files on my server, but i didn’t wanted to give a vi on every of them.
Solution:
As far as i know, there should be an easier solution for this, but this is the one i’ve found so far:
command | xargs -n1 command1
Example:
find ./ -depth -name .htaccess | xargs -n1 vi