My favorites | Sign in
Project Logo
                
Search
for
Updated Oct 20, 2009 by stefan.petrea
scripts  
One-sentence summary of this page.

Various scripts

This page contains scripts or settings wich are useful to me(may be useful to you too).

1) Mirroring two subversion repositories located on different servers

cd ~/perlhobby			    ;svn update;
cd ~/firepower_repo/google_code_repo;svn update;
rsync -avr --exclude .svn --delete ~/perlhobby/ ~/firepower_repo/google_code_repo/
svn add *
cd ~/firepower_repo/google_code_repo;
svn commit -m "";

2) Bash Function to get your ip from console and put it in your clipboard

myip () {
        sudo ifconfig | perl -ple 'print $_ if /inet addr/ and $_ =~ s/.*inet addr:((?:\d+\.){3}\d+).*/$1/g  ;$_=""' | grep -v ^\s*$ | head -1 | xsel -i
}

3) Move all files containing a certain word to a directory created with the name of that word

select_and_move () {
        perl -e 'my $a= shift @ARGV; @stuff = `find | grep -i $a | cut -b3-`;$stuff[$_]=~s/^(.*)\n$/\"$1\"/g for 0..@stuff-1;`mkdir $a`;`mv $_ $a` for @stuff;' $1
}

Application : Classifying very fast large ammounts of unstructured/unsorted files which are sitting in one place.

4) Searching recursively in a directory for a file that contains a pattern

grep -r pattern *

Variation (Only see the files in which at least one match occurs) : grep -lir pattern *

5) Zipping the current directory and sending it to the desktop of some computer

#just zips the whole project , transfers it to the laptop
#for easy reach
rm project.zip
cd ..
svn export duplicate_file_eraser /tmp/duplicate_file_eraser
cd duplicate_file_eraser/
zip -r  project.zip /tmp/duplicate_file_eraser/
rm -rf /tmp/duplicate_file_eraser
scp project.zip stef@laptop:~/Desktop/
rm project.zip

6) Unpacking the contents of a tar directly from a computer to the other

Description of the problem:

You have two machines.

On one of them(A) you have a big archive.

On the other(B) you have enough space to store the unpacked contents of the archive.

We consider for the purpose of the example the biggest file in archive located on A.

Solution:

on B do:

sudo nc -l -p 100 >  biggest_file_in_archive

on A do:

tar Oxvf bigarchive.tar biggest_file_in_archive | nc Ip_of_B 100

This will pipe(redirect) all STDOUT from tar output to the nc which will send them

to B,on B we have a server opened that writes everything to disk

in a file called biggest_file_in_archive

References

http://drkmario.blogspot.com/2007/02/using-netcat.html

7) Sending as attachment to separate emails the files in a directory

ls | perl -ne '$a=$_;chomp $a;$b="mutt -s \"poza $a\" -a $a <email> < message.txt\n";print $b;`$b`'

8) A totally fast ,lame and unsecure way to quickly ssh when ssh keys are not working(for whatever reasons)

echo "use the clipboard" 
echo "<password>" | xsel -i
ssh -X -p 22 root@<IP_OF_MACHINE_TO_CONNECT>

9) using rsync over a custom port

rsync -av --progress --inplace --rsh='ssh -p<port>' user@host:<remote_path> <local_path>

other things

for finding the length of a file without downloading it

curl -I <url to file>

all cpan mirrors and how to modify the currently set mirror

allot of mirrors can be found here

get in /etc/perl/CPAN/Config.pm   and see under urllist

10) Using screen with horizonta/vertical splits

Instructions on how to get and install screen with vertical split http://scie.nti.st/2008/8/22/gnu-screen-with-vertical-split-support

Or you can just get the binary here (and hopefully it'll work)

Advantage: allows to see the output of many console applications at the same time

Drawback : the layout of the vertical/horizontal splits is not preserved throughout sessions

Screen detach/attaches screen -x

command what it does
ctrl +a V vertical split region
ctrl +a S horizontal split region
ctrl +a c opens a new bash shell in vertical/horizontal split
ctrl +a X close a region
ctrl +a
<tab>
switches between regions
ctrl +a :resize +N to resize a region by adding N lines to it
ctrl +a :resize -N to resize a region by shortening it with N lines

At reference 3) there is a non-working way for preserving layout of a screen.

References

11) Converting djvu->pdf and splitting pdfs

converting djvu->pdf:

aptitude install djvulibre-bin libtiff-tools libtiff-dev
ddjvu -format=tiff file.djvu file.tiff
tiff2pdf -o file.pdf file.tiff

splitting pdfs

pdftk file.pdf cat begin_page-end_page output output_file.pdf

(you can also use "end" instead of end_page)

converting pdf -> jpg

convert -resize 1600x1200 -density 500 file.pdf file.jpg

12) Have bash tell you how much emails you received exactly after you log on

print_email_count_in_folder() {
    echo -e "\E[35m {  "`find ~/mail/ | grep new | grep $1 | perl -ne '$a = $_;chomp $a; print $_ unless -d $a' | wc -l`" new emails in folder [$1]     }"
    tput sgr0
}

check_emails() {
    print_email_count_in_folder "directory_name1"
    print_email_count_in_folder "director_name2"
}

13) Replace occurences of strings in multiple files

perl -pi -e 's/searched_string/replacement_string/' *.ext

Note: recursive search&replace

find * | xargs perl -p -i -e s/OldText/NewText/g
or
perl -p -i -e 's/search_string/replace_string/g' `grep -ril search_string *`

recursive search&replace of filenames

find ./ -exec rename 's/pattern/replacement/' * "{}" \;

14) Some vim tips

1 chaning from string1 -> string2 and string2 -> string1

s/string1\|string2/\=submatch(0) == "string1" ? "string2" : "string1"/g

shortcuts

key combination what it does
* finds the next occurence in the file of the word the curor is currently sitting on
ctrl+a n creates a new window
ctrl+a
<arrow>
selects a different window

ctags

requirements:

  1. install vim-full package
  2. install exuberant-ctags package

command what it does
vi −t tag Start vi and position the cursor at the file and line where "tag" is defined.
:ta tag Find a tag.
Ctrl-] Find the tag under the cursor.
Ctrl-T Return to previous location before jump to tag (not widely implemented).

15) Converting chm to pdf

instructions on how to install chm2pdf

16) Some settings from Debian

file purpose
/etc/init.d/rc.local setting commands to run on boot-time
/etc/resolvconf/resolv.conf.d/original keeps track of DNSs. sidenote: /etc/resolv.conf flushes them at reboot . sudo resolvconf -a eth1 can be used to modify them or modifying the original file can also do the same

17) Tightvnc settings

You need two packages tightvncserver,vnc4server and

vncconfig iconic=true nowin=true -display localhost:<display_number>

This setting is for making the clipboard guest-host OS transfer work.

You can find the display number with

ps xau | grep `whoami` | grep Xtightvnc

You can start the vnc server with

vncserver -compatiblekbd

and it will tell you what display number it has associated (the switch is for alt-key problems)

and then connect to it with vncviewer by writing vncviewer < ip_address > : < display_number>

Note: if you have scroll lock pressed your alt key may not work

18) Mirroring websites

Mirror starting from a location and not going more than 3 links deep,disabling the possibility to go on different sites/domains/tld

httrack --depth=3 --ext-depth=0 --stay-on-same-dir --stay-on-same-tld --stay-on-same-domain <link>

19) Finding out what application binds a specific port

As root run

netstat -plant | grep ':<port_number>'

20) Renaming a group of files

find . -exec mv {} {}.<new_extension> \;

21) Getting peak-hours for irssi logs

cat \#perl.log | perl -ne '/^(\d{2}):/; $hour[$1]++;  END{ print "$_: $hour[$_]\n" for sort {$hour[$a] <=> $hour[$b]} 0..23;   }'

Sign in to add a comment
Hosted by Google Code