Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Console / shell tips & tricks
View unanswered posts
View posts from last 24 hours

Goto page Previous  1, 2, 3 ... 9, 10, 11 ... 14, 15, 16  Next  
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
rhill
Retired Dev
Retired Dev


Joined: 22 Oct 2004
Posts: 1629
Location: sk.ca

PostPosted: Sat Dec 18, 2004 11:15 am    Post subject: bash alias tips and tricks Reply with quote

hey all

i've seen some very cool tricks done with bash aliases here and there, and i thought i'd start this thread for people to show off and share some of their bashrc creations.

i'll guess start the ball rolling with my extremely amateur collection:

Code:
# -=[aliases]=-

# the basics
alias ls='ls --color=auto'
alias ll='ls --color -lah'
alias cp='cp -vg'
alias mv='mv -vg'
alias rm='rm -v'
alias df='df -ha'
alias du='du -hc'
alias logout='clear && logout'
alias exit='clear && exit'
alias grep='grep --color=auto'
alias zgrep='zgrep --color=auto'

alias ..='cd .. && ls'
alias cl='clear && ls'

alias targz='tar xvfz'
alias tarbz='tar xvfj'

alias efind='equery list -p'

# config
alias nnx='sudo nano /etc/X11/xorg.conf'
alias nnm='sudo nano /etc/make.conf'
alias nnpk='nano /etc/portage/package.keywords'
alias nnpu='nano /etc/portage/package.use'
alias nnb='nano ~/.bashrc'

alias srcenv='sudo /usr/sbin/env-update && source /etc/profile && source ~/.bashrc'

# debug
alias m4chk='egrep -L "AC_DEFUN\(\[.*," /usr/share/aclocal/*'  #note to self: learn sed already
alias makedirty='sudo chown -R dirtyepic:users /home/dirtyepic/ && sudo chown -R root:portage /etc/portage'
alias thisismine='sudo chown -R dirtyepic:users *'
Back to top
View user's profile Send private message
Gherald
Veteran
Veteran


Joined: 23 Aug 2004
Posts: 1399
Location: CLUAConsole

PostPosted: Sat Dec 18, 2004 11:27 am    Post subject: Reply with quote

a couple more:
Code:
alias less="less -r" #color
alias more="less -r" #color
alias findf=_findf #find files
function _findf
{
        find . | grep -i "$@"
}

also while it's not exactly an alias I like to have a small executable file called redo in my path somewhere:
Code:
$ cat ~/bin/redo
history | grep $* | tail

what it does is show you the last 10 commands you've done that contain a given string, for instance try
Code:
redo grep

then you can "!###" where ### is the history number of the command, and it will be repeated

I find this more useful than CTRL+R because it gives a list of choices, rather than just one.
Back to top
View user's profile Send private message
Irom
Tux's lil' helper
Tux's lil' helper


Joined: 07 Oct 2003
Posts: 93
Location: am arsch..

PostPosted: Sat Dec 18, 2004 1:02 pm    Post subject: Reply with quote

freeix wrote:
alias less="less -r" #color
You can do this with less. Just append the parameter to the environment variable LESS (export LESS=${LESS}r or in /etv/env.d).

freeix wrote:
alias findf=_findf #find files
function _findf
{
find . | grep -i "$@"
}

You don't need a function here:
Code:
alias findf='find . | grep -i "$@"'

And there is no also no need for grep:
Code:
alias findf='find -iname "$@"'
But -iname has a slightly different behaviour than grep.

I use aliases only for things I'm too lazy to type and functions for the fancy stuff.
Code:

# finds big files (50MB with no parameter)
findbig()
{
  local meg="${1-50}"
  find -size +$(($meg*1024))k -exec du -h {} \; 2>/dev/null
}

# extracts each zip-file in the current directory to its own directory
zipdir()
{
  local i
  local x
  for i in *zip;do x="${i%%.zip}";mkdir "$x";cd "$x";unzip ../"$i";cd ..;done
}

_________________
http://ftp.fukt.bsnet.se/pub/movies/stallman/ (Please watch this before you form an opinion about GNU)
https://apfelboymchen.net/gnu/
Back to top
View user's profile Send private message
Gherald
Veteran
Veteran


Joined: 23 Aug 2004
Posts: 1399
Location: CLUAConsole

PostPosted: Sat Dec 18, 2004 1:21 pm    Post subject: Reply with quote

Irom wrote:
You don't need a function here:
Code:
alias findf='find . | grep -i "$@"'

That doesn't work.
Quote:
And there is no also no need for grep:
Code:
alias findf='find -iname "$@"'
But -iname has a slightly different behaviour than grep.

Yes, I prefer grep.

findbig and zipdir are nice, thanks.

it's worth mentioning that GREP_COLOR can be set to something other than bright red which can be hard to read. I like:

Code:
export GREP_COLOR=31


EDIT: formatting


Last edited by Gherald on Sat Dec 18, 2004 1:28 pm; edited 3 times in total
Back to top
View user's profile Send private message
stahlsau
Guru
Guru


Joined: 09 Jan 2004
Posts: 582
Location: WildWestwoods

PostPosted: Sat Dec 18, 2004 1:23 pm    Post subject: Reply with quote

here a few of mine:
Code:
alias ls="ls --color=always"
alias halt="sudo /sbin/halt"
alias reboot="sudo /sbin/reboot"
alias confcat="/home/xxx/scripts/confcat"
alias n="/bin/nano"
alias pgrep="pgrep -l"
alias s="/usr/bin/scite"
alias df="df -Pkh"
alias x="startxfce4"
alias grep='grep --color=auto'
alias s="/usr/bin/scite"
alias uloc="slocate -l 0 -c -u"
alias loc="slocate -l 0 -i"
D="`date|tr -d " "`"
alias ss="sleep 3 && import -window root shot_$D.jpg"
export PS1="\[\033[01;32m\]\u:\w>\[\033[00m\]"
alias beep="beep -f 500 -r 2 -n -r 4 -l 10 --new -l 300"
alias resume="emerge --resume"
alias emresk="emerge --resume --skipfirst"
alias update="emerge -auvD world"
Back to top
View user's profile Send private message
odegard
Guru
Guru


Joined: 08 Mar 2003
Posts: 324
Location: Trondheim, NO

PostPosted: Sat Dec 18, 2004 1:51 pm    Post subject: Reply with quote

Code:

alias r='history | grep'
alias l='ls -alh --color'
alias .='cd ..'
alias nano='nano --smooth'
alias xf='startxfce4'
alias du='du -h'
alias df='df -h'


Some of mine. Browsing your files is superfast with 'l' for dir-listing and just '.' to go up one level. I really recommend you to try it.

Also, I don't use any DE manager så I boot into bash and hit 'xf' to start xfce4.
Back to top
View user's profile Send private message
Gherald
Veteran
Veteran


Joined: 23 Aug 2004
Posts: 1399
Location: CLUAConsole

PostPosted: Sun Dec 19, 2004 12:28 pm    Post subject: Reply with quote

odegard wrote:
Code:
alias .='cd ..'

That is destructive, seeing as . is already a builtin equivalent to "source" (try ". /etc/profile" ". ~/.bashrc" etc...)

I saw a .bashrc on the web with the following, which I think is preferable:
Code:
alias 1="cd ../"
alias 2="cd ../.."
alias 3="cd ../../.."
alias 4="cd ../../../.."
alias 5="cd ../../../../.."
Back to top
View user's profile Send private message
oberyno
Guru
Guru


Joined: 15 Feb 2004
Posts: 467
Location: /bin/zsh

PostPosted: Sun Dec 19, 2004 12:52 pm    Post subject: Reply with quote

A few from my zshrc, but most of these should work in bash.
Code:
alias gimp="/usr/bin/gimp-[0-9].[0-9]"
alias mplayer="mplayer $@ 2>/dev/null 1>&2"
alias psgrep="ps aux | grep -v 'grep --color=auto -e %MEM' | grep -e %MEM -e"
alias d="du -sh"
alias df="df -h"
alias dirs="dirs -v"
alias free="free -m"
alias ccheck="CCACHE_DIR=/var/tmp/ccache ccache -s"

# I dislike having the extra ,v on rcs files.
for c in co ci rlog rcs rcsclean rcsdiff rcsmerge ; do
    alias $c="$c -x"
done
unset c

# cd around dirstack with just - and +. "-" by itself overrides the precommand modifier "-".
alias -- +='pushd +0 1>/dev/null'
alias -- -='pushd -1 1>/dev/null'
for var in {1..$((DIRSTACKSIZE -1 ))}; do
    alias -- -$var="cd -$var 1>/dev/null"
    alias -- +$var="cd +$var 1>/dev/null"
done
unset var

and some zsh specific aliases:
alias '..'='cd ..'
alias -g ...='../..'
alias -g ....='../../..'
alias -s com='firefox'
alias -s html='firefox'
alias mmv="autoload -U zmv; noglob zmv -Wv"
alias zcalc="autoload -U zcalc; zcalc"
Back to top
View user's profile Send private message
Irom
Tux's lil' helper
Tux's lil' helper


Joined: 07 Oct 2003
Posts: 93
Location: am arsch..

PostPosted: Sun Dec 19, 2004 1:09 pm    Post subject: Reply with quote

freeix wrote:
Irom wrote:
You don't need a function here:
Code:
alias findf='find . | grep -i "$@"'

That doesn't work.

Strange, it works for me :?:

But I just found out that the "$@" is useless too because the alias just gets replaced with the content of the alias.
_________________
http://ftp.fukt.bsnet.se/pub/movies/stallman/ (Please watch this before you form an opinion about GNU)
https://apfelboymchen.net/gnu/
Back to top
View user's profile Send private message
Gherald
Veteran
Veteran


Joined: 23 Aug 2004
Posts: 1399
Location: CLUAConsole

PostPosted: Sun Dec 19, 2004 1:12 pm    Post subject: Reply with quote

Ah, so it works without the "$@"

thanks
Back to top
View user's profile Send private message
electrofreak
l33t
l33t


Joined: 30 Jun 2004
Posts: 713
Location: Ohio, USA

PostPosted: Thu Dec 23, 2004 3:38 am    Post subject: Reply with quote

Updike!

Code:
alias updike='/usr/bin/uptime | perl -ne "/(\d+) d/;print 8,q(=)x\$1,\"D\n\""'


This takes your uptime and for each day adds length to the...um.... :lol:
_________________
Desktop: ABit AN8, Athlon64 X2 4400+ 939 2.75GHz, 2x1GB Corsair XMS DDR400, 2x160GB SATA RAID-0, 2x20"W, Vista Ultimate x64
Laptop: 15.4" MacBook Pro 2.4Ghz, 2x1GB RAM, 160GB, Mac OS X 10.5.1
Server: PIII 550Mhz, 3x128MB RAM, 160GB, Ubuntu Server 7.10
Back to top
View user's profile Send private message
Gherald
Veteran
Veteran


Joined: 23 Aug 2004
Posts: 1399
Location: CLUAConsole

PostPosted: Thu Dec 23, 2004 3:57 pm    Post subject: Reply with quote

:lol:
Back to top
View user's profile Send private message
sigSEGV2003
Tux's lil' helper
Tux's lil' helper


Joined: 11 Mar 2003
Posts: 81
Location: Kansas

PostPosted: Thu Dec 23, 2004 8:55 pm    Post subject: Reply with quote

I can't live without

Code:
alias lls='ls -l --sort=size --reverse'
alias llt='ls -l --sort=time --reverse'
Back to top
View user's profile Send private message
odegard
Guru
Guru


Joined: 08 Mar 2003
Posts: 324
Location: Trondheim, NO

PostPosted: Thu Dec 23, 2004 10:36 pm    Post subject: Reply with quote

freeix wrote:
odegard wrote:
Code:
alias .='cd ..'

That is destructive, seeing as . is already a builtin equivalent to "source" (try ". /etc/profile" ". ~/.bashrc" etc...)
[/code]


I never had any problem with it...? It works according to my intention.
Back to top
View user's profile Send private message
TrueDFX
Retired Dev
Retired Dev


Joined: 02 Jun 2004
Posts: 1348

PostPosted: Fri Dec 24, 2004 7:54 am    Post subject: Reply with quote

odegard wrote:
I never had any problem with it...? It works according to my intention.
It doesn't cause problems directly, but it will break when you source a script which uses . to source another script.
Back to top
View user's profile Send private message
Irom
Tux's lil' helper
Tux's lil' helper


Joined: 07 Oct 2003
Posts: 93
Location: am arsch..

PostPosted: Fri Dec 24, 2004 3:07 pm    Post subject: Reply with quote

TrueDFX wrote:
odegard wrote:
I never had any problem with it...? It works according to my intention.
It doesn't cause problems directly, but it will break when you source a script which uses . to source another script.
This shouldn't be a problem. Why would you want to source a script in an interactive shell?

Another tip: aliases can be temporarily circumvented by prepending a backslash. I often need this for my "ls"-alias.
_________________
http://ftp.fukt.bsnet.se/pub/movies/stallman/ (Please watch this before you form an opinion about GNU)
https://apfelboymchen.net/gnu/
Back to top
View user's profile Send private message
mcspiff
Tux's lil' helper
Tux's lil' helper


Joined: 24 Oct 2004
Posts: 109

PostPosted: Fri Dec 24, 2004 3:21 pm    Post subject: Reply with quote

alias emerge="sudo emerge"
alias svim = "sudo vim"

security is for the weak.
Back to top
View user's profile Send private message
spb
Retired Dev
Retired Dev


Joined: 02 Jan 2004
Posts: 2135
Location: Cambridge, UK

PostPosted: Fri Dec 24, 2004 5:34 pm    Post subject: Reply with quote

My /root/.bashrc consists of the following:
Code:
alias p='cd /etc/security/selinux/src/policy'

That and emerge are basically all I use root for anyway.
Back to top
View user's profile Send private message
TrueDFX
Retired Dev
Retired Dev


Joined: 02 Jun 2004
Posts: 1348

PostPosted: Fri Dec 24, 2004 7:12 pm    Post subject: Reply with quote

Irom wrote:
This shouldn't be a problem. Why would you want to source a script in an interactive shell?
Because sourced scripts may modify environment variables. Scripts to set up the correct PATH, LD_LIBRARY_PATH, etc. for a set of applications rely on this.
Back to top
View user's profile Send private message
Gherald
Veteran
Veteran


Joined: 23 Aug 2004
Posts: 1399
Location: CLUAConsole

PostPosted: Tue Mar 08, 2005 10:38 pm    Post subject: Reply with quote

odegard wrote:
freeix wrote:
odegard wrote:
Code:
alias .='cd ..'
That is destructive, seeing as . is already a builtin equivalent to "source" (try ". /etc/profile" ". ~/.bashrc" etc...)
I never had any problem with it...? It works according to my intention.

I came up with:

Code:
function . {
    if [ $# -eq 0 ]; then #do something useful if given no arguments
        unalias -a
        for config in /etc/profile ~/.bash_profile ~/.bash_alias ~/.bashrc; do
            [ -e $config ] && source $config
        done
    else #built-in default behavior
        source $@
    fi
}


Of course, for what you want it would be:
Code:
function . {
    if [ $# -eq 0 ]; then #do something useful if given no arguments
        cd ..
    else #built-in default behavior
        source $@
    fi
}
Back to top
View user's profile Send private message
odegard
Guru
Guru


Joined: 08 Mar 2003
Posts: 324
Location: Trondheim, NO

PostPosted: Thu Mar 10, 2005 11:35 pm    Post subject: Reply with quote

TrueDFX wrote:
Irom wrote:
This shouldn't be a problem. Why would you want to source a script in an interactive shell?
Because sourced scripts may modify environment variables. Scripts to set up the correct PATH, LD_LIBRARY_PATH, etc. for a set of applications rely on this.


I guess you're right, I'm no expert but I've used this for almost 2 years now and I've never experienced any problems with it.
Back to top
View user's profile Send private message
ahubu
Guru
Guru


Joined: 16 Aug 2003
Posts: 400
Location: Groningen, The Netherlands

PostPosted: Wed Sep 21, 2005 3:23 pm    Post subject: Reply with quote

Looks like this thread was lost in oblivion

It has given me some great commands, so it's worth the bump I think. People got anymore tricks up their sleeve? Or an alias?

Some to get you started ;)
Code:

alias dvd2iso='dd if=/dev/hdc of=/media2/iso/temp/dvdimage.iso'
alias lsa='ls *.zip *.arj *.bz2 *.rar *.ace *.tar *.gz *.lzh *.sit 2>/dev/null'
alias lsm='ls *.mpg *.avi *.ogm *.mpeg *.MPEG *.AVI *.MPG *.OGM *.asf *.ASF *.wmv *.WMV 2>/dev/null'
alias psa='ps aux |grep'

_________________
Anne // Light travels faster than sound. That's why people appear bright until
you hear them speak.
-Unknown
Back to top
View user's profile Send private message
lynxnyl
Apprentice
Apprentice


Joined: 15 Aug 2004
Posts: 253
Location: Ljubljana, Slovenija

PostPosted: Wed Sep 21, 2005 4:17 pm    Post subject: Reply with quote

no need to use psa - you have pgrep and pkill. ;)
And another useful ps option is --forest.
Back to top
View user's profile Send private message
ahubu
Guru
Guru


Joined: 16 Aug 2003
Posts: 400
Location: Groningen, The Netherlands

PostPosted: Wed Sep 21, 2005 8:17 pm    Post subject: Reply with quote

lynxnyl wrote:
no need to use psa - you have pgrep and pkill.


Well, I like to see for myself what the name of the process is that I kill. Particularly, when I have some python programs running, I need to kill one of them, I don't know the exact name, I use 'psa python' for a quick look. pgrep only outputs the process numbers, doesn't give me what process belongs to it.


And since you mentioned the --forest flag, I should mention the 'pstree' command :)
_________________
Anne // Light travels faster than sound. That's why people appear bright until
you hear them speak.
-Unknown
Back to top
View user's profile Send private message
lynxnyl
Apprentice
Apprentice


Joined: 15 Aug 2004
Posts: 253
Location: Ljubljana, Slovenija

PostPosted: Wed Sep 21, 2005 8:42 pm    Post subject: Reply with quote

well, then at least extend it to:
Code:

alias psa='ps aux |grep -v grep |grep'

;)

pstree, nice. There is still use for those wierd chars. :) Looks more clean and classy than the forest.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Goto page Previous  1, 2, 3 ... 9, 10, 11 ... 14, 15, 16  Next
Page 10 of 16

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum