Showing posts with label Linux Hacks. Show all posts
Showing posts with label Linux Hacks. Show all posts

Friday, March 29, 2013

Sort,uniq (string, int) columns in csv files


cat file

text1,5
text4,3
text3,1
text1,4
text7,2
text3,7
text2,9


1. less file | sort -u -t, -k1,1 - sort the first column and get unique texts
text1,5
text2,9
text3,1
text4,3
text7,2

2. less file | sort -u -t, -k1,1 | sort -t, -k2 -nr - get unique texts on first column and reverse order for the second column using -r option
text2,9
text1,5
text4,3
text7,2
text3,1

Tuesday, September 11, 2012

Download a web site using wget

wget -r --level=0 -convert-links --page-requisites --no-parent <URL>
httrack <URL>

Monday, December 5, 2011

Remove a folder except some files

mkdir folder1
touch file1, file2, file3, file4
rm -rf -- !(@(file1|file2)*)

Saturday, December 3, 2011

“cd -” to toggle between the last two directories

ubuntu1004@ubuntu1004:~$ cd /tmp/tmp1/tmp2/tmp3
ubuntu1004@ubuntu1004:/tmp/tmp1/tmp2/tmp3$ cd /tmp/tmp10/tmp20/tmp30
ubuntu1004@ubuntu1004:/tmp/tmp10/tmp20/tmp30$ cd -
/tmp/tmp1/tmp2/tmp3
ubuntu1004@ubuntu1004:/tmp/tmp1/tmp2/tmp3$ cd -
/tmp/tmp10/tmp20/tmp30
ubuntu1004@ubuntu1004:/tmp/tmp10/tmp20/tmp30$ cd -
/tmp/tmp1/tmp2/tmp3
ubuntu1004@ubuntu1004:/tmp/tmp1/tmp2/tmp3$

mkdir on subfolders and cd

Add in .bash_profile
function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\""; }


ubuntu1004@ubuntu1004:~$ mkdircd /tmp/tmp1/tmp2/tmp3
ubuntu1004@ubuntu1004:/tmp/tmp1/tmp2/tmp3$ pwd
/tmp/tmp1/tmp2/tmp3

CDPATH defines base directory for cd command

ubuntu1004@ubuntu1004:~$ pwd
/home/ubuntu1004
ubuntu1004@ubuntu1004:~$ cd init.d
bash: cd: init.d: No such file or directory
ubuntu1004@ubuntu1004:~$ export CDPATH=/etc
ubuntu1004@ubuntu1004:~$ cd init.d
/etc/init.d
ubuntu1004@ubuntu1004:/etc/init.d$ pwd
/etc/init.d
ubuntu1004@ubuntu1004:/etc/init.d$

- change permanent, add export CDPATH=/etc to your ~/.bash_profile
- you can add more than one directory entry in the CDPATH variable, separating them with ':' 
export CDPATH=.:~:/etc:/var