Misc Mac and bash tips

See also https://gist.github.com/raggleton

Profile bash script

This will print out time, line number & content so you can see how long each line takes:

PS4='$(date "+%s.%N ($LINENO) + ")' bash -x scriptname

Taken straight from http://stackoverflow.com/questions/4336035/performance-profiling-tools-for-shell-scripts

Change the dark blue in ls

The dark blue that is used when listing directories when one does
is utterly unreadable on a black background. Change it by doing:


Find and replace in bash commands

To repeat the last command but replacing (the first occurence of) a word e.g

crab -get 1,3,5 -c MyJob

The latter command will replace "get" with "replace" and carry out the command.

To replace every occurrence of a word


Find all files of a type and display their sizes

e.g. find all ROOT files and show how big they are in human readable form:

 find . -name "*.root" -print0 | xargs -0 ls -sh 

The "." is the path to start from (it'll search recursively) The -print0 and -0 are to avoid crashes if there are spaces in names The ls print the file size (-s) in human readable form (-h)

Taken from: http://ubuntuforums.org/showthread.php?t=1059187

Find out size of folder

For a folder and all its subfolders:

du -h <folder name>

For just the total size of a folder, don't list sub folders: du -sh <folder name>


Run commands on lxplus, even when disconnected!

Use either: nohup (easy but can't regain control), screen (can regain control, but doesn't seem to like CMSSW?)

Run bash command at regular intervals

Use the watch command:

watch -d -n 60 "ls *.root"

Show duplicate lines

awk 'seen[$0]++ == 1' <filename>

From http://www.adp-gmbh.ch/unix/awk/printing_duplicate_lines_only_once.html

Copying files to/from over ssh


Local -> Remote

 scp somefile username@lxplus.cern.ch:~/wherever

Remote -> Local

scp username@lxplus.cern.ch:/home/username/file_name ~/file-name 

~/.bash_profile settings

Use this to run things when you logon, set alias etc. (Not ~/.bashrc as that's for local access I think... see http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html)


# User specific environment and startup programs


export PATH

export WORK=/afs/cern.ch/work/r/raggleto
alias ll="ls -l"
alias la="ls -a"
alias lh="ls -lh"
alias lha="ls -lha"

# Source env files
# MUST be in order: UI, CMSSW, CRAB

source /afs/cern.ch/cms/LCG/LCG-2/UI/cms_ui_env.sh
cd $WORK/scratch0/CMSSW_5_3_5/src
source /afs/cern.ch/cms/ccs/wm/scripts/Crab/crab.sh

Using delete key in emacs

Add this line to your ~/.emacs file

(normal-erase-is-backspace-mode 1)

Colour terminal prompt

Edit ~/.bash_profile (or maybe .bashrc?) and add the lines:

export CLICOLOR=1
export PS1="\[\033[36m\][\t]\[\033[m\]:\[\033[32;1m\]\w\[\033[m\] >> "

This gives [time]:[current directory] > as a prompt. The \[ \] around ANSI escapes stops it from looping round and overwriting the prompt. For more options:




Deleting empty files

find [Optional* starting directory name] -type f -empty -delete -- RobinAggleton - 25-Oct-2012
Edit | Attach | Watch | Print version | History: r16 < r15 < r14 < r13 < r12 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r16 - 2014-04-30 - RobinAggleton
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    Main All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2021 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
or Ideas, requests, problems regarding TWiki? use Discourse or Send feedback