LinuxPlanet Blogs

By Linux Geeks, For Linux Geeks.

Archive for the ‘.bashrc’ Category

My .bashrc File Part 4 – Alias’

without comments

If you are unfamiliar with the alias command then you are missing out on an effective and efficient tool. Alias is a way to create a shortcut command in bash. You can take very long commands with several options or triggers and create your own shorthand equivalent of that long command. For Example, for me to get the weather for my local area using the weather-util application I would normally need to type:

weather -f --id=KSLC -c "Salt Lake City" -s UT

Well, that's way to long. I normally only want to know the weather in my local area, so I've created an alias for this long command.

alias weather='/usr/bin/weather -f --id=KSLC -c "Salt Lake City" -s UT'

This command essentially substitutes the longer command to display the weather for Salt Lake City with just the command "weather". I have several alias' in my .bashrc file to make my life easier.

# some ls aliases

alias ll='ls -l'

alias la='ls -A'

alias l='ls -CF'

#Information

alias weather='/usr/bin/weather -f --id=KSLC -c "Salt Lake City" -s UT'

alias gcalcli='gcalcli --cals all'

#For getting around

alias videos='cd /home/jared/Videos/ && ls'

alias pics='cd /home/jared/Pictures/ && ls'

alias music='cd /home/jared/Music/ && ls'

alias podcasts='cd /home/jared/Podcasts/ && ls'

alias dropbox='cd ~/Dropbox/ && ls'

alias documents='cd ~/Documents/ && ls'

alias notes='cd ~/Notes/ && ls'

alias manti='cd ~/Manti/jared/ && ls'

The first group of alias' are very common shortcuts for the "ls" command. The second group of alias' give me shortcuts for the weather and gcalcli applications which displays the weather and my google calendar information. Finally, the las group gives me shortcuts to commonly used directories. This saves me tons of time as I don't have to type out the complete path of a commonly used directory.

Please share some of your cool alias' that you have implemented.

Written by Jared

March 8th, 2010 at 8:00 am

My .bashrc File Part 3 – Making Life Easier

without comments

A very common thing to do in the command line is to extract compressed files. Now as many of you probably know there are a dozen different compression methods out there. Let's say I have a .bz2, .7z, .gz and a .rar file and I don't want to think about which app to use and what option I need to extract my file. I just want my file uncompressed. Let's make life a little easier and insert the following into your .bashrc file.

#------Extraction of compressed files--------------
# from ARCH Wiki

extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "don't know how to extract '$1'..." ;;
esac
else
echo "'$1' is not a valid file!"
fi
}

Now when you need to uncompress a file, simply type "extract filename" and slam-bam-thank-you-ma'am. No thinking or looking up the correct syntax, all you are left with is an uncompressed file and isn't that all we really wanted?

What fun things are you hiding in your .bashrc file that makes life easier? Please share.

Written by Jared

March 4th, 2010 at 8:00 am

My .bashrc File Part 2 – Useful System Information

without comments

In the last segment I discussed displaying some useful personal information using a function script within my .bashrc file. Today I'll discuss displaying useful system information. I have two funtions which display system information, which is set up very similar to my previous script.



This first script displays my internal IP address and my WAN IP address by simply extracting that information from ifconfig whenever I type "myip" in the terminal. Here is the script.




function myip() # get IP adresses
{
MY_IP=$(/sbin/ifconfig eth0 awk "/inet/ { print $2 } " sed -e s/addr://) \ MY_ISP=$(/sbin/ifconfig
eth0 awk "/P-t-P/ { print $3 } " sed -e s/P-t-P://)
}




This displays:




External IP:
xxx.xxx.xxx.xxx
Internal IP:
192.168.0.97




This second function displays my machine statistics, file system space available, memory stats and IP information, when I type "ii" in the terminal. Here is the function:




function ii() # get current host related info
{
echo -e "\nHello ${RED}$USER"
echo -e "\nSystem information:$NC " ; uname -a
echo -e "\n${RED}Machine stats :$NC " ; uptime
echo -e "\n${RED}Storage stats :$NC " ; df -h grep -v varrun grep -v varlock grep -v udev grep -v\ devshm
echo -e "\n${RED}Memory stats :$NC " ; free -m
echo -e "\n${RED}Local IP Address :$NC" ; ifconfig grep
'inet addr:' grep -v '127.0.0.1' cut -d: -f2 awk '{ print $1}'
echo -e "\n${RED}ISP Address :$NC" ; wget
www.whatismyip.com/automation/n09230945.asp -O - -q
echo ""
}



You'll notice that in this script, my outside WAN IP is pulled from a website instead of from the ifconfig command. Here is the output:




Hello jared

System information:
Linux manti 2.6.24-24-server #1 SMP Fri Sep 18 17:24:10 UTC
2009 i686 GNU/Linux


Machine stats :
10:02:17 up 81 days, 15:14, 10 users, load average: 0.15, 0.38, 0.37

Storage stats :
Filesystem
Size Used Avail Use% Mounted on

/dev/sda1 17G 3.0G 13G 19% /
/dev/sda2 276G 246G 16G 94% /home
/dev/sdb1 187G 146G 32G 82% /media/backup

Memory stats :
total used free shared buffers cached
Mem: 502 492 10 0 37 65
-/+ buffers/cache: 389 113
Swap: 1874 30 1844

Local IP Address :
192.168.0.97
ISP Address :

xxx.xxx.xxx.xxx


I apologize that not everything is lined up correctly, it's due to the layout in blogger. The output lines up fairly cleanly in the terminal.

Enjoy!

Written by Jared

March 3rd, 2010 at 8:00 am

My .bashrc File Part 1 – Useful Information

without comments

I thought it would fun to have a short series regarding my .bashrc file. Feel free to share any cool aspects or insights from your .bashrc file during this series. A short disclaimer for this series, regarding most of what I will be sharing with you is NOT original content. I've copied most aspects of my .bashrc from others who have been so kind to share their cool ideas. Now, lets begin.

I want to focus the first apart of this series on having bash display useful information in an easily accessible and concise way. For example, the first thing I do each morning is run my "update" script which displays the time, my schedule for the day and the weather, which looks something like this:

Hello jared, How are you today?

Current Date and Time:

Fri Feb 26 09:00:40 MST 2010

Your Schedule:
Mon Feb 22 12:00am Zone 4
Fri Feb 26 12:00am Safety Kids Fair

6:30am Scriptures
11:00am Preschool time
Sat Feb 27 9:30am Food Co-op Pick up

Current Weather Conditons:

Current conditions at UT (KSLC)
Last updated Feb 26, 2010 - 10:53 AM EST / 2010.02.26 1553 UTC
Wind: from the SSE (160 degrees) at 13 MPH (11 KT)
Sky conditions: mostly cloudy
Temperature: 35.1 F (1.7 C)
Relative Humidity: 69%
City Forecast for Salt Lake City, UT


Here is the script I use in my .bashrc file.

function update() # Current date, time, weather and calendar
(
echo -e "\nHello $USER, How are you today?"
echo -e "\nCurrent Date and Time: $NC " ; date
echo -e "\nYour Schedule: $NC " ; gcalcli agenda head
echo -e "\nCurrent Weather Conditons: $NC " ; weather head -n 7
echo ""
)

Let me run through this function. As you can see it's all just echo commands spewing out the information I desire. I begin by having bash say "Hello" to me. Just because it's a shell doesn't mean it can't be polite. Next it displays the current date and time which is followed by my schedule for the day. My schedule is produced by a cool little app called gcalcli which was a Google summer of code project that displays your google calendar on the command line. I blogged about this app previously here. I then finish things up with the weather using the weather-utils application. I have the weather command set up as an alias (which I'll cover in another segment) to display my local weather.

As mentioned previously, this is the first command I type into the terminal when I first wake up, which gives me a quick and concise outlook for the day.

Well, I hope this was interesting to some of you. My next installment will cover displaying useful system information. So until then... enjoy!

Written by Jared

March 1st, 2010 at 8:00 am