Shell Cheatsheet
Change default editor
sudo rc-update-alternatives --config editor
Get ID of window
pidgin_pid=`pgrep -x pidgin`
echo "pidgin is running with PID $pidgin_pid"
pidgin_window_id=`wmctrl -l -p | awk '{print $1, $3}' | grep " $pidgin_pid"`
echo "pidgin is using window $pidgin_window_id"
Check if a process is already running
pidgin_count=`pgrep -c -x pigin`
if [ "$pidgin_count" -ne 0 ]; then
echo "the app is already running." 1>&2
echo "Please close it before executing this command." 1>&2
exit 1
fi
Get current directory
$ pwd
Check if file exists
if [! -d "/path"]; then fi
Check if directory eixsts
if [! -f "/path"]; then fi
In your bash script, set automatic non zero exit on error
set -e
Run command over ssh without keeping connection open
$ `ssh DX@192.168.1.110 'ls ~/'`
Converting scripts or files to proper unix equivalent (remove \r)
$ dos2unix filename
Go to previous directory
$ cd -
Get stack trace information from core dump file
$ gdb /usr/bin/myapp.binary coredumpfile (gdb) bt (gdb) bt full (gdb) info threads (gdb) thread apply all bt (gdb) thread apply all bt full
Save file permissions to file then restore
# save file permission $ getfacl -R dirname > filname.perm # restore file permission $ setfacl --restore=filname.perm #this is useful for instance if you are using svn which resets your file permissions
Get list of modules loaded
$ module list
View and save output to file at the same time
# redirect in correct order $ ./script.sh 2>&1 | tee mylog.log # all error output followed by all normal output $ ./script.sh | tee mylog.log 2>&1 # logs all normal and error output in correct order $ ./script.sh > mylog.log 2>&1 # log only normal output and display only error output $ (./script.sh > mylog.log) 2>&1 # log only normal output and display only error output $ ./script.sh 2>&1 > mylog.log # log normal and error output in correct order but display nothing $ (./script.sh 2>&1) > mylog.log
Concatenate string and variable in shell script
MY_VARIABLE="myvalue"
MY_SECOND_VARIABLE=${MY_VARIABLE}/add_text
Forward system mails received
$ echo "forwardtothisemail@email.com" > $HOME/.forward $ chmod 644 $HOME/.forward
Get PID of running process
# (this might look through all processes for any user) $ ps -ef | grep processname | grep -v grep | sed -e 's%^[a-z][a-z]* *%%' -es% .*$%%' # (to get it for a specific user and process name) $ pgrep -u mrassi -f processname
List running processes and kill specific
(this might look through all processes for any user) $ps -ux $kill -9 pid
Check if specific application is in your path
$ which app
Add home library path to your environment
# add line: export LD_LIBRARY_PATH=$(HOME)/lib to below file in the export section $ vi ~/.env/user.bashrc
Set permissions for all files in directory
find /path/to/dir/ -type f -exec chmod 644 {} \;
Find file
# Find file with name myFilename.txt starting from current directory down $ find -name "myFilename.txt" # Find any file with txt extension $ find -name "*.txt" # Find fileName with any extension $ find -name "fileName.*" # Find myFilename.txt starting in /home/mrassi $ find /home/mrassi -name "myFilename.txt"