Category Archives: Bash

How To Determine Directory of Sourced Bash Script

Sourcing a script can be useful for exporting variables in the current shell. However, you can’t use “dirname $0″ to get the directory of the script. Here’s how to get the same effect of dirname, but still allowing you to … Continue reading

Posted in Bash, Linux | Leave a comment

How to Fix an Empty Bash Shell Prompt

If you bash shell prompt is empty when logging in, here’s how to fix it. Create a ~/.bashrc file: if [ -f /etc/bashrc ]; then . /etc/bashrc fi Create a ~/.bash_profile file: if [ -f ~/.bashrc ]; then . ~/.bashrc … Continue reading

Posted in Bash, Linux | Leave a comment

How to Convert Seconds to Minutes in BASH

This is how to convert seconds into HH:MM:SS in bash: $ date -d ’1970-01-01 100 sec’ +’%H:%M:%S’ 00:01:40

Posted in Bash | Leave a comment

How to Monitor Multi-Core CPU Usage on Linux

Here’s how to monitor multiple cores on Linux: # mpstat -P ALL Example: Monitor all CPU’s in 1 second intervals forever # mpstat -P ALL 1

Posted in Bash, Linux, Monitoring | Tagged | Leave a comment

How To Get Yesterday's Date using BASH Shell Scripting

The new short way: $ date -d ’1 day ago’ +’%Y/%m/%d’ 2009/07/21 Or the longer way: Yesterday in epoch seconds $ yesterday=$((`date +’%s’` – 86400)) Get default formatted yesterday’s date $ date -d “1970-01-01 $yesterday sec” Tue Feb 17 01:27:32 … Continue reading

Posted in Bash, Linux | 2 Comments

How to View a Specific Page or Section of Manpage

For example, the “kill” manpage shows: SEE ALSO bash(1), tcsh(1), kill(2), sigvec(2), signal(7) This is how to view section 7 of the “signal” manpage: man 7 signal

Posted in Bash, Linux | Leave a comment

Faster File Copy Using Tar and SSH

How to quickly copy a file or directory over SSH using tar: tar -cf – myfile | ssh target_host tar -xf – If you want to change the target directory, use this: tar -cf – myfile | ssh target_host “cd … Continue reading

Posted in Bash, Linux | 1 Comment

Copying directory trees with tar pipe tar

# tar cf – source_dir | ( cd target_dir && tar xBf – )

Posted in Bash, Linux | Leave a comment

How to get the directory location of a shell script

dirname=`dirname $0`

Posted in Bash | Tagged | Leave a comment