bash_handy_commands

USEFUL LINKS

UNIX commands
How to Write a Simple Makefile?
Makefiles for Dummies(video)

at

unix submit at example job on the certain time
[ http://kb.iu.edu/data/aewo.html]
at -m 4:00 <atjob
rm the at job: at -r <atjob.number>


crontab

Run jobs at regular intervals
Crontab –e #open a new/current crontab
Format :

Field Meaning Allowed range Example
1 Minutes that have to pass after the selected hour in order to execute the task 0-59 30, which means 30 minutes after the selected
2 Hours at which the task has to be executed 0-23 04, which means at 4 O'clock in the morning
3 Days of the month on which this task has to be executed 1-31 *, which means that every day of the selected month
4 Months during which the task has to be executed 1-12 3-5, which means run the task in the months of March, April & May First 3 letters of the Month name. Case doesn't matter. E.g. Jan
5 Days of the week on which this task has to be run 0-7 * means all days of the selected weeks Numeric value or first 3 letters of the Day name. Case doesn't matter (Sun or sun) (0 or 7 is Sun, 1 is Mon…)
6 Name of the program (task) to be executed Any program absolute path to executable required

Example:

0,30 21-22 31 1 * bash /scratch/hpc/danial/replica_relaxation/UMSRE/hole_polymer/ crontab_submitter.x

[http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/]
[http://stackoverflow.com/questions/878600/how-to-create-cronjob-using-bash]
[http://www.softpanorama.org/Utilities/cron.shtml]
[http://www.ibm.com/developerworks/linux/library/l-job-scheduling/index.html]


for/seq/jot

#!/bin/bash
for (( c=1; c<=5; c++ ))
do
    echo "Welcome $c times..."
done

OR
#!/bin/bash
for i in $(seq 1 2 20)
do
   echo "Welcome $i times"
done

in mac there is no "seq" and you have to use jot:
$ for i in `jot -p 3 20 1 2` ;do ; echo $i ;done
1.000
1.053
1.105
1.158
1.211
1.263
1.316
1.368
1.421
1.474
1.526
1.579
1.632
1.684
1.737
1.789
1.842
1.895
1.947
2.000

git

a very good tutorial for git
another good ref. for git
git cheat sheet
Commands:

git add
git commit
git push
git pull
git status
#switching to another branch
git checkout -b amber12-with-branches origin/amber12-with-patches
git log #Viewing the Commit History
git reset --hard HEAD~1 # Undo last Git commit for more info -> http://stackoverflow.com/questions/927358/undo-last-git-commit
git branch -d <branch name> #deleting the branch name

...

ls

ls with exclusion

ls -I 'job*'

mkdir

making multiple folder

mkdir run/folder_{1..10}
### in bash 4 and after
mkdir run/folder_{001..10} ## this make sorting easier

qsub

If you wished to submit it to run after job 1234 exits successfully, you can submit it as such, assuming the jobscript is called job.pbs:

qsub -W depend=afterok:1234 job.pbs

ps

give you the list of active process on the cpu

>ps aux
danial   16964  0.0  0.0   5372  1436 pts/10   S+   17:54   0:00 gnuplot_x11
danial   17118  0.0  0.0  69840  1044 pts/6    R+   18:03   0:00 ps aux
danial   23564  0.0  0.1 149224 31328 pts/1    T    Aug23   0:01 /usr/local/encap/R-2.5.0/lib64/R/bin/exec/R
danial   24035  0.0  0.1 112940 26740 pts/3    S+   Aug23   0:00 /usr/local/encap/R-2.5.0/lib64/R/bin/exec/R
68       25408  0.0  0.0  12324   848 ?        S    Aug29   0:00 hald-addon-keyboard: listening on /dev/input/event1
danial   25713  0.0  0.0 305232 14328 ?        S    Aug29   0:00 file-roller --extract-to=/home/danial/libs/python --extract-here /home/danial/libs/python/numpy-1.7.1.tar.gz
danial   25717  0.0  0.0  67244  2456 pts/4    Ss+  Aug23   0:00 -csh
danial   25758  0.0  0.0 305208 14112 ?        S    Aug29   0:00 file-roller --extract-to=/home/danial/libs/python --extract-here /home/danial/libs/python/numpy-1.7.1.tar.gz
danial   25989  0.0  0.1 152600 30980 pts/1    S+   Aug23   0:01 /usr/local/encap/R-2.5.0/lib64/R/bin/exec/R
danial   28209  0.0  0.0 305088 14212 ?        S    Aug29   0:00 file-roller --extract-to=/home/danial/script/namd_scripts

Also if you are on the network you can find your current processes using the following commands:
ps -ef |grep {username}

rsync

rsync is used to perform the backup operation in UNIX / Linux.

rsync -azvp --progress hpc:/scratch/hpc/danial/folder/* ./backup

rsync with exception:
update everything except '*.log'

rsync --exclude '*.log' -avz -e ssh myname@servername:foldertocpy .

http://www.thegeekstuff.com/2010/09/rsync-command-examples/


sed

sed -i 0,/startxxx/{s/startxxx/start/} file.dat  ####replace only the first occurrence
sed -i s/startxxx/start/g file.dat  ####replace all occurrences

sort

example, sorting file listing by number

>ls move.job0.* | sort -n -t . -k 3
move.job0.1.sort.colvars.traj
move.job0.2.sort.colvars.traj
move.job0.3.sort.colvars.traj
move.job0.4.sort.colvars.traj
move.job0.5.sort.colvars.traj
move.job0.6.sort.colvars.traj
move.job0.7.sort.colvars.traj
move.job0.8.sort.colvars.traj
move.job0.9.sort.colvars.traj
move.job0.10.sort.colvars.traj

where,
-n: sorts numerically
-t: field separator '.'
-k: sort on third field, in this case the numbers after the first '.'

su

su <user_name> #login to your account from another login


tr

tr utility copies the given input to produced the output with substitution or deletion of selected characters.

echo 'I LovE                  linuX. one is    better Than 2' | tr -s ' ' ' '
##I LovE linuX. one is better Than 2

echo 'I LovE linuX. one is better Than 2' | tr "a-z" "A-Z"
##I LOVE LINUX. ONE IS BETTER THAN 2

more information can be found here.

touch

touch file_name
echo -n "" > file_name #rewrite if the file exist


vi/vim

color syntax highlighting

:syntax on
:syntax off
ENABLE COLOR SYNTAX HIGHLIGHTING PERMANENTLY: in "$HOME/.vimrc" add "syntax on".

Highlight all search pattern matches

:set hlsearch
:nohlsearch

Python setting

add following to ~/.vimrc

syntax on
set autoindent
set tabstop=4
set expandtab
set shiftwidth=4
filetype indent on

zip/tar

You need to use tar command as follows (syntax of tar command):
tar -zcvf archive-name.tar.gz directory-name
Where,
• -z: Compress archive using gzip program
• -c: Create archive
• -v: Verbose i.e display progress while creating archive
• -f: Archive File name

To unzip a file that is only compressed with bz2 use
bunzip2 filename.bz2
to unzip things tar are compressed with .tar.bz2 use
tar -xvjpf filename.tar.bz2
tar xvfz filename.tar.gz
[http://www.simplehelp.net/2008/12/15/how-to-create-and-extract-zip-tar-targz-and-tarbz2-files-in-linux/]


Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License