Tikejhya: Ashish Nepal

Knowledgebase

Category: BASH (Page 2 of 7)

print all but first two column

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

will print all but very first column:

cat somefile | awk '{$1=""; print $0}'

will print all but two first columns:

cat somefile | awk '{$1=$2=""; print $0}'

curl with –data

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

curl -s -A “Ashish /1.0 (Linux; Centos 1.1.1; TK-LOL Build/10000)” –data “username=tikejhya” –data “password=mypass” –location “http://ashishnepal.com/login”

how to test memcached object from commandline or telnet

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

echo -e ‘get cache_objectn’; sleep 3 | telnet 192.168.1.1 11211 | grep -i VALUE

 

Memcached Tips:

Telnet to your server:

telnet 127.0.0.1 11211

List the items, to get the slab ids:

stats items
STAT items:3:number 1
STAT items:3:age 498
STAT items:22:number 1
STAT items:22:age 498
END
The first number after ‘items’ is the slab id. Request a cache dump for each slab id, with a limit for the max number of keys to dump:

stats cachedump 3 100
ITEM views.decorators.cache.cache_header..cc7d9 [6 b; 1256056128 s]
END

stats cachedump 22 100
ITEM views.decorators.cache.cache_page..8427e [7736 b; 1256056128 s]
END

screen session on non root user

postgres@ip-10-73-209-228:~$ ps
PID TTY TIME CMD
31309 pts/3 00:00:00 bash
31338 pts/3 00:00:00 ps

root@ip-10-73-209-228:~# ls -al /dev/pts/4
crw——- 1 root tty 136, 4 Jun 9 16:17 /dev/pts/4
root@ip-10-73-209-228:~# chown postgres /dev/pts/4
root@ip-10-73-209-228:~# ls -al /dev/pts/4
crw——- 1 postgres tty 136, 4 Jun 9 16:17 /dev/pts/4
root@ip-10-73-209-228:~#

 

now you will be able to run screen.

rename multiple files – bash

sometimes you might need to rename multiple files,
e.g you have 100’s files called something.something_12 and we want to keep name removing_12 from end.

You may want to use mv or rename command to do so
# for file in *_12; do mv $file ${file/_12/} ; done;

change ownership recursively

find dir -type f -exec chmod 644 {} ‘;’ # make all files rw-r-r-
To change permissions bits of all directories:

find dir -type d -exec chmod 755 {} ‘;’ # make all directories rwxr-xr-x
It would be nice if you could just do this:

Bash script to recursively add a file folder inside directory

This is just a sample script which will allow you to create directories inside directory for depth of 3 folders, with a file in each folder in 3rd directory.

ie.
1/2/3
3/4/3 …. and so on.

#!/bin/bash
cd dir
for ((i=0;i<=9;i+=1)); do mkdir -p $i cd $i; touch index.html for ((j=0;j<=9;j+=1)); do mkdir -p $j cd $j; touch index.html for ((k=0;k<=9;k+=1)); do mkdir -p $k; touch index.html done cd ../ done cd ../ done

Taking off one level off, which creats only 2 level

#!/bin/bash
cd dir
for ((i=0;i<=9;i+=1)); do mkdir -p $i cd $i; touch index.html for ((j=0;j<=9;j+=1)); do mkdir -p $j; touch index.html done cd ../ done

getent hosts vs hosts

There may be some occasion that you want to bypass hostfile to resolve public IP, how it works?

here is small example, host command works exactly as dig. (ie. doesnot do byhostname lookup)

host www.ashishnepal.com -> will look directly to DNS (not looking host file same as dig)
getent hosts www.ashishnepal.com -> will look into /etc/hosts file first and go to DNS.

null routing

Deny access using null routing
ip route add blackhole $1

Allow access to those who got blocked
ip route del $1

shell-init: error retrieving current directory: getcwd: cannot access parent directories:

shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
chdir: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory

This is not a bash problem. You’re in a directory which can be read and
searched only by root, and you’re su’ing to another user. That user
does not have permission to search the current directory, even to find
`..’, so getcwd is going to fail.

su –

Page 2 of 7

Powered by WordPress & Theme by Anders Norén