preload
Dez 20

If you want to download a file with curl type the following code into bash:

1
curl -O http://www.url.de/file.html

-O means to output to a file and not to stdout, normally the screen.

What if you need to resume a download of a big file?

1
curl -C - -O http://www.url.de/file.html

With -C – you continue downloading the current file. If is important to write -C -. Don’t forget the -

You can also set a referrer and the user agent.

1
curl -C - -O -A http://www.url.de -e "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; de; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6" http://www.url.de/file.html

With -A you can set the referrer and -e sets the user agent.

Pretty helpful.

Tagged with:
Dez 02

To scroll in screen-mode of linux type the following:
Strg+a then AltGr+8

Now you can scroll up and down. This is called the copy-mode.

Source: http://blog.pregos.info/2009/02/26/scrollen-im-screen/

Tagged with:
Nov 25

To replace a string in all files inside a folder you can create a bash file:

1
vi replace.sh

Put inside this file:

1
2
3
4
5
6
#!/bin/bash
for file in $(ls -1);
do
sed 's/me/you/g' $file > /tmp/dummy.txt;
mv /tmp/dummy.txt $file;
done

Save it and make it executeable (chmod 777 replace.sh)

In this case the string “me” will be replace with “you” in all files of the actual folder and the files will be overridden.
Another example
Your want to replace the tag <b> with <strong> in all files inside a special folder. Further you want to backup the original files with suffix .old and the new files will be overridden.
Simply type on the console:
1
2
3
4
5
for file in $(ls -1);
do
mv $file $file.old
sed 's/&lt;b&gt;/&lt;strong&gt;/g' $file.old &gt;  $file;
done
You can modify the script(s). Maybe by adding parameters etc.

Another option is to use sed with -i command or direct editing:

1
2
3
4
for file in $(ls -1);
do
sed -i 's/text_to_replace/text_replacement/g' $file
done
Tagged with:
Nov 25

People often underestimate the usefulness of the linux-command screen.

What does screen do? Screen creates a terminal session for the actual user.

Type:

1
screen

to create a terminal session.

Or type:

1
screen -S <sessionname>

where <sessionname> should be replaced with your own name.

Create a new window:

1
Crtl+a c

which means (Press Control and a together and after that press c)

Now we created a second window. To list all created windows in the actual session type:

1
Crtl+a "

Germans have to press Shift+2 for “.

Now the two created windows will be shown:

Bildschirmfoto 2009-11-25 um 14.55.07

Just choose one Window and press enter.

To quit your actual work an detach the session:

1
Crtl+a d

To list all active sessions:

1
screen -list

You should see a similar picture like the following:

Bildschirmfoto 2009-11-25 um 15.03.27

Here you can see the name of the session: 1597.stefan

To restore the session simply type:

1
screen -r stefan

Maybe you have to type the complete id with 1597.stefan if there exist another session with the same name.

Tagged with:
Nov 19

When switching from Mac to Linux and vice versa, I always have to think a bit about how to use the command top and sort by CPU.

With Linux it is

top

and then press

shift+p

Mac:

top -o cpu

I hope I can remember this :-)

Tagged with:
Nov 16

To list all open connections on the actual linux or mac just type the following:

lsof -i

For faster lookup without replacing IP through DNS-lookup:

lsof -i -n

Try also with root-permission for more files:

sudo lsof -i -n

Now you can easily spot programs connecting to the internet an dump the tcp-data with tcpdump.

For example dumping data from tcp-port 80 to a file called test.pcap:

tcpdump -w test.pcap tcp port 80

Now show the file:

tcpdump -nnr test.pcap

or

tcpdump -ttttnnr test.pcap

for timestamp information

Tagged with: