Tuesday, September 27, 2011

Multiple sed expression in one line

To do multiple sed expression in one line, you can use -e a few times according to your need. For example:

To subtitute 'little' with 'big' and 'lamb' with 'cow' in "Mary had a little lamb" we can use:

$ echo 'Mary had a little lamb' | sed -e 's/little/big/' -e 's/lamb/cow/'
Mary had a big cow
where -e is for expression flag, 's/little/big/' is the first expression and 's/lamb/cow/' is the second expression

That's all.

Friday, September 23, 2011

Simple http server using python

This tip is really useful when you need a really quick web server running without too much hassle. Python comes with http server built-in that you can activate in a matter of seconds if you have python already installed. If you haven't install it, you might need an additional few minutes to install it. Below are the steps (this is assuming you are using redhat/fedora linux):

1. open a terminal and install python

# yum install python
2. Once done, browse to any directory that you want to be your document root directory, in this example I'll use /home/myuser
# cd /home/myuser
3. Run the below command to start the simple http server, where 8000 is the port number the server will use
# python -m SimpleHTTPServer 8000
Serving HTTP on 0.0.0.0 port 8000 ...
4. Now open a browser, and type http://192.168.1.2:8000 to access the server, where 192.168.1.2 is your machine's ip address

Monday, August 15, 2011

Download apache directory listings recursively

To apache directory listings recursively, use wget like below:

$ wget -r -np -nH -R index.html http://filestodownload/

where -r is for recursive retrieving, -np is for no-parent option where wget won't get the parent directory when retrieving recursively, -nH equals to no host diretories where generation of host-prefixed directories will be disabled and -R is to omit index.html.



Friday, August 5, 2011

Extract rpm without installing

Run below command:

$ rpm2cpio packagename.rpm | cpio -idmv 
where rpm2cpio will change rpm file to cpio, -i is for extract, -d is to create leading directories where needed, -m is to preserve modification time and -v is for verbose mode.

Thursday, July 28, 2011

Creating a lot of same sized files

Let's say you need to create 10 files with 100kB size. All you need to do is create a 1MB files and split it into 10 files.

$ dd if=/dev/zero of=myfile bs=1024 count=1024
$ ls -lh
-rw-rw-r--. 1 owner owner 1.0M Jul 28 15:33 myfile
where if is input file, of is output file, bs is byte size and count is the multipler of bs

and then
$ split -b 100k myfile x
$ ls -lh
-rw-rw-r--. 1 owner owner 100K Jul 28 15:35 xaa
-rw-rw-r--. 1 owner owner 100K Jul 28 15:35 xab
-rw-rw-r--. 1 owner owner 100K Jul 28 15:35 xac
-rw-rw-r--. 1 owner owner 100K Jul 28 15:35 xad
-rw-rw-r--. 1 owner owner 100K Jul 28 15:35 xae
-rw-rw-r--. 1 owner owner 100K Jul 28 15:35 xaf
-rw-rw-r--. 1 owner owner 100K Jul 28 15:35 xag
-rw-rw-r--. 1 owner owner 100K Jul 28 15:35 xah
-rw-rw-r--. 1 owner owner 100K Jul 28 15:35 xai
-rw-rw-r--. 1 owner owner 100K Jul 28 15:35 xaj
-rw-rw-r--. 1 owner owner 24K Jul 28 15:35 xak
where -b is byte size, myfile is your file input name and x is your prefix.