Making your own commands!

Every day solutions to every day challenges. + Brilliant stuff

Moderators: b1o, jkerr82508

Forum rules
Please feel free to post your tip it does not have to be advanced. Also ask questions directly related to the tip here. But do not start new threads with questions or ask for help here. That is what the help section is for. forum rules: http://bjoernvold.com/forum/viewtopic.php?f=8&t=568
User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Making your own commands!

Postby viking60 » 09 Nov 2011, 01:36

We have those brilliant gurus here and they often help me with brilliant solutions. At that moment i am on top of the world and the next time it happens, and I need that brilliant command again I ......have.....forgotten it :oops:
I know it is only me, but the only way for me is to write it down here and hope that I come across it later, so here we go:

One brilliant RPM command is

Code: Select all

rpm -qa --last | less
It shows what you installed when nicely sorted by time and date with the latest installation on top.
This way you can check what that last auto-update put on your box. Or if you are like me, what you yourself put on the box last. I use it a lot in Mandriva.

Well.... if I can remember it,
If not: I come here to the tip section and find it nicely documented - and my problem is solved.
But there has to be a better way!
Imagine if I could make a two letter command like li for "last installed" out of that? What a dream!

....Although we fix the impossible immediately here - dreams take somewhat longer, but here we go:

Code: Select all

alias li="rpm -qa --last | less"
:dance1
Now I only have to open a terminal and write li

You can do this with pretty much any command if you always make a typo on ls you can do a

Code: Select all

alias sl=ls

After that both commands will work.
.....Until you end the session or until you do a

Code: Select all

unalias sl

You can play around with it and automate the command you need and put it in:
~/.bashrc
Like so:
alias li="rpm -qa --last | less"
alias sl=ls

After you have added those lines they will work forever.
Nice isn't it?
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
rolf
Guru-Berserk
Posts: 1107
Joined: 16 Mar 2010, 16:07

Re: Making your own commands!

Postby rolf » 09 Nov 2011, 02:33

+1 Very nice.

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Making your own commands!

Postby viking60 » 09 Nov 2011, 10:07

Yeah and it can make life easier for those newly converted Windows users that always like to type dir to list the contents of a directory (or folder as they call it):

Code: Select all

alias dir='ls -l'

Et voila; the Windows user feels at home in Linux +1
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
dubigrasu
Gaming-Berserk
Posts: 81
Joined: 20 Nov 2010, 15:59

Re: Making your own commands!

Postby dubigrasu » 09 Nov 2011, 11:54

I personally like to use aliases with explicit names. I like commands like "whereis" or "whoami", first time I use those I felt like talking with Hal :)
If you are to make your own commands why replace a cryptic command with a cryptic alias? Besides, (if rarely used) after a while I will probably forget not only the command but also the alias for it.
So for an alias that's suppose to show the last installed packages I would use exactly that; "lastinstalled". Is a long command yes, but there is always the Tab key to help you ;)

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Making your own commands!

Postby viking60 » 09 Nov 2011, 12:30

Yes you have got a point! I never forget the mysql_secure_installation command, even if it is long enough. But the beauty of this command is that we can adapt it to our sometimes very exotic logic. :-D
(Who came up with urpmi?!!! :shock: )
But this is brilliant! Do give examples of your favorite aliases!
So that I can ruthlessly steal them. :whistle:
(...Stealing lastinstalled now .... cause it is better :-D )

I also find home more logical than cd ~ so I did a:

Code: Select all

alias home=cd ~

And while I am on a role:

Code: Select all

alias copy="cp -i"

The i switch will prompt you it the file exists, to overwrite it. And the same goes for move and mv -i.
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
dubigrasu
Gaming-Berserk
Posts: 81
Joined: 20 Nov 2010, 15:59

Re: Making your own commands!

Postby dubigrasu » 09 Nov 2011, 13:36

Ahem...I'm afraid my aliases are not always making sense for someone else...they are very...let's say personalized ;)
For example a while ago instead of using a long complicated/boring command to start the tvtuner with some parameters (a command that of course I forgot now and it was very easy to screw up) I used the alias:

Code: Select all

iamamoron

I don't think you want to steal that :-D
Or for this command which I used frequently:

Code: Select all

svn checkout http://pcsx2.googlecode.com/svn/trunk/ pcsx2-read-only && cd pcsx2-read-only && mkdir my_build && cd my_build && cmake ../CMakeLists.txt -DFORCE_INTERNAL_SDL=TRUE -DCMAKE_BUILD_TYPE=Release && cd .. && make && make install

I used also a not making sense alias:

Code: Select all

showmethemoney


Why showmethemoney ? I have no idea, it just popped into my head :confused

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Making your own commands!

Postby viking60 » 09 Nov 2011, 13:45

Clearly bit coin inspired.
:A

I have edited some commands above and here is a list for those good ol' DOS guys:

clr clear
cls clear
copy cp -i
del rm -i
delete rm -i
dir ls -alg
home cd ~
ls ls -F
md mkdir
move mv -i
pwd echo $cwd
type more

But I am still wondering what command to assign to Apfelstrudel and Pannekaker - It has to be something good :think:
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
rolf
Guru-Berserk
Posts: 1107
Joined: 16 Mar 2010, 16:07

Re: Making your own commands!

Postby rolf » 09 Nov 2011, 15:35

Maybe this is helpful. To see the currently defined aliases, um alii... :berserk2

Code: Select all

$ alias
alias cd..='cd ..'
alias cp='cp -i'
alias d='ls'
alias df='df -h -x supermount'
alias du='du -h'
alias egrep='egrep --color'
alias fgrep='fgrep --color'
alias grep='grep --color'
alias l='ls'
alias la='ls -a'
alias ll='ls -l'
alias ls='ls -F --color=auto'
alias lsd='ls -d */'
alias mc='. /usr/lib64/mc/mc-wrapper.sh'
alias md='mkdir'
alias mv='mv -i'
alias p='cd -'
alias rd='rmdir'
alias rm='rm -i'
alias s='cd ..'

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Making your own commands!

Postby viking60 » 09 Nov 2011, 16:52

Wow! I knew that Gurus would use this. And here I only discoverd it today while you have been using it the whole time! :-D
(You should have told us normal mortals).

I did not know about this one:

Code: Select all

alias cd..='cd ..'

And it has bothered me before. I bet they put that space in there just to annoy me :f I will definitely steal this one! +1
And you have been hiding this one from me too:

Code: Select all

alias df='df -h -x supermount'

Naughty naughty :naughty:
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
rolf
Guru-Berserk
Posts: 1107
Joined: 16 Mar 2010, 16:07

Re: Making your own commands!

Postby rolf » 09 Nov 2011, 17:01

Well, yes, I did see these things once or twice. To be sure, these are not aliases that I made; they are there in the default Mandriva installation.

Code: Select all

[rolf@localhost ~]$ cat /etc/profile.d/60alias.sh
#!/bin/sh
# Linux-Mandrake configuration: Chmouel Boudjnah <chmouel@mandrakesoft.com>
#
# Common Aliases for a system.
#
# The Semantic is :
#       If exist a ~/.alias and the user hasn't specified a
#       LOAD_SYSTEM_ALIAS variables then don't do any system aliases
#       If there is no ~/.alias but the user has specified a
#       IGNORE_SYSTEM_ALIASES then don't do any system aliases.

[ -f ~/.alias ] && [ -z $LOAD_SYSTEM_ALIASES ] && return 0
[ -n "$IGNORE_SYSTEM_ALIASES" ] && return 0

if [ -f ~/.dir_colors ]; then
        eval `dircolors --sh ~/.dir_colors`
else
        eval `dircolors --sh /etc/DIR_COLORS`
fi

# Don't define aliases in plain Bourne shell
[ -n "${BASH_VERSION}${KSH_VERSION}${ZSH_VERSION}" ] || return 0

# default ls options
LS_OPTIONS="-F"

# emacs doesn't support color
if [ $TERM != "emacs" ];then
    LS_OPTIONS="$LS_OPTIONS --color=auto"
fi

# Note that you should not add custom aliases here as this file will be
# overwritten when package bash is upgraded. Instead create an own profile
# file for any added aliases.

alias ls="ls $LS_OPTIONS"

alias d="ls"                                                 
alias l="ls"                    # classical listing.
alias ll="ls -l"                # List detailled.
alias la='ls -a'        # List all.               
alias lsd="ls -d */"            # List only the directory.
alias cd..="cd .."
alias s="cd .."
alias p="cd -"

alias md="mkdir"
alias rd="rmdir"
alias cp="cp -i"
alias mv="mv -i"
alias rm="rm -i"

alias grep="grep --color"      # colored grep by default
alias egrep="egrep --color"      # colored grep by default
alias fgrep="fgrep --color"      # colored grep by default

# Size of a directory (by default Human Readable).
alias du='du -h'                             
                                             
# Size of a disk (by default Human Readable).
# and don't probe supermount
alias df='df -h -x supermount'
[rolf@localhost ~]$ rpm -qf /etc/profile.d/60alias.sh
bash-4.2-9-mdv2011.0.x86_64


So, these aliases come from Mandriva's version of bash, iiuc. :berserk2

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Making your own commands!

Postby viking60 » 09 Nov 2011, 17:09

Ah yes I see - I have the 60alias.sh file too but it does not work out of the box here. I have put all my commands in .bashrc where they do work. But I have modyfied my .bashrc before so that might be the reason....
# .bashrc

# User specific aliases and functions

# Source global definitions
#if [ -f /etc/bashrc ]; then
# . /etc/bashrc
#fi
# Check for an interactive session
#[ -z "$PS1" ] && return

#alias ls='ls --color=auto'
#PS1='[\u@\h \W]\$ '
bash_prompt_cmd() {
RTN=$?
#The superduper Kernel spped patch
#if [ "$PS1" ] ; then
# mkdir -m 0700 /sys/fs/cgroup/cpu/user/$$
# echo $$ > /sys/fs/cgroup/cpu/user/$$/tasks
# fi
#alias liste etter tid over hva som ble installert sist
alias lastinstalled="rpm -qa --last | less"
# andre aliaser
alias cd..='cd ..'
alias df='df -h -x supermount'

#Her kommer smiley
smiley() {
if [ $1 == 0 ] ; then
echo ":)"
else
echo ":("
fi
}
smileyc() {
if [ $1 == 0 ] ; then
echo $GREEN
else
echo $RED
fi
}
if [ $(tput colors) -gt 0 ] ; then
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
RST=$(tput op)
fi
smiley=$(smiley $RTN)
local BL="\[\e[1;34m\]"
local WH="\[\e[1;37m\]"
local BR="\[\e[0;33m\]"
local RE="\[\e[1;31m\]"
local PROMPT="${CY}$"
[ $UID -eq "0" ] && PROMPT="${RE}#"

# Add the first part of the prompt: username,host, and time
local PROMPT_PWD=""
local PS1_T1="$BL.:[ $CY`whoami`@`hostname` $BL: $CY\t $BL:$CY "
local ps_len=$(( ${#PS1_T1} - 12 * 6 + 6 + 4 )) #Len adjust for colors, time and var
local PS1_T2=" $BL]:.\n\[\$smileyc\]\$smiley\[$RST\] "
local startpos=""

PROMPT_PWD="${PWD/#$HOME/~}"
local overflow_prefix="..."
local pwdlen=${#PROMPT_PWD}
local maxpwdlen=$(( COLUMNS - ps_len ))
# Sometimes COLUMNS isn't initiliased, if it isn't, fall back on 80
[ $maxpwdlen -lt 0 ] && maxpwdlen=$(( 80 - ps_len ))

if [ $pwdlen -gt $maxpwdlen ] ; then
startpos=$(( $pwdlen - maxpwdlen + ${#overflow_prefix} ))
PROMPT_PWD="${overflow_prefix}${PROMPT_PWD:$startpos:$maxpwdlen}"
fi
export PS1="${PS1_T1}${PROMPT_PWD}${PS1_T2}"
}
PROMPT_COMMAND=bash_prompt_cmd

I think i need to steal dubi's iamamoron :-D Do you have a reference to 60aliases.sh in your .bashrc?
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
rolf
Guru-Berserk
Posts: 1107
Joined: 16 Mar 2010, 16:07

Re: Making your own commands!

Postby rolf » 09 Nov 2011, 18:36

viking60 wrote:Do you have a reference to 60aliases.sh in your .bashrc?


No. There is a little function, e, that I wrote for an envelope printing program. :A I don't remember how I use fp :confused

However, since alias is a bash thing, I looked first at /etc/bashrc, which refers to /etc/profile, and that is where the scripts in /etc/profile.d are called, afaict. :berserkf

Code: Select all

[rolf@localhost ~]$ cat .bashrc
# .bashrc

# User specific aliases and functions

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi





function e() { /home/rolf/envelope/envelope $1|lpr -P MFC620CN; }
#function e() { /home/rolf/envelope/envelope $1|kprinter -d tp0 --nodialog; }
function fp() { efix -ops $1|kprinter --stdin; }

#/usr/sbin/anacron -t /home/rolf/etc/anacrontab

GPG_TTY=âÂÂttyâÂÂ
export GPG_TTY

## start Lennart's solution
#if [ "$PS1" ] ; then
#       mkdir -m 0700 /dev/cgroup/cpu/user/$$ > /dev/null 2>&1
#       echo $$ > /dev/cgroup/cpu/user/$$/tasks
#        echo "1" > /dev/cgroup/cpu/user/$$/notify_on_release
#fi
## end Lennart's solution
[rolf@localhost ~]$ cat /etc/bashrc
# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# It's NOT good idea to change this file unless you know what you
# are doing. Much better way is to create custom.sh shell script in
# /etc/profile.d/ to make custom changes to environment. This will
# prevent need for merging in future updates.

# By default, we want this to get set.
# Even for non-interactive, non-login shells.
if [ "`id -gn`" = "`id -un`" -a `id -u` -gt 99 ]; then
        umask 002
else
        umask 022
fi

# are we an interactive shell?
if [ "$PS1" ]; then
    case $TERM in
        xterm*)
            PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'
            ;;
        screen)
            PROMPT_COMMAND='echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\033\\"'
            ;;
        *)
            ;;
    esac
    [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
    # You might want to have e.g. tty in prompt (e.g. more virtual machines)
    # and console windows
    # If you want to do so, just add e.g.
    # if [ "$PS1" ]; then
    #   PS1="[\u@\h:\l \W]\\$ "
    # fi
    # to your custom modification shell script in /etc/profile.d/ directory
   
    if [ -z "$loginsh" ]; then # We're not a login shell
        # Not all scripts in profile.d are compatible with other shells
        # TODO: make the scripts compatible or check the running shell by
        # themselves.
        if [ -n "${BASH_VERSION}${KSH_VERSION}${ZSH_VERSION}" ]; then
            for i in /etc/profile.d/*.sh; do
                if [ -r $i ]; then
                    . $i
                fi
            done
            unset i
        fi
    fi
fi

unset loginsh
[rolf@localhost ~]$ cat /etc/profile
# /etc/profile -*- Mode: shell-script -*-
# (c) MandrakeSoft, Chmouel Boudjnah <chmouel@mandrakesoft.com>

loginsh=1

if [ "$UID" -ge 500 ] && ! echo ${PATH} |grep -q /usr/games ; then
    PATH=$PATH:/usr/games
fi

umask 022

USER=`id -un`
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
HISTCONTROL=ignoredups
HOSTNAME=`/bin/hostname`
HISTSIZE=1000

if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ]; then
    INPUTRC=/etc/inputrc
fi

# some old programs still use it (eg: "man"), and it is also
# required for level1 compliance for LI18NUX2000
NLSPATH=/usr/share/locale/%l/%N

export PATH PS1 USER LOGNAME MAIL HOSTNAME INPUTRC NLSPATH
export HISTCONTROL HISTSIZE

for i in /etc/profile.d/*.sh ; do
        if [ -r $i ]; then
                . $i
        fi
done

unset i
[rolf@localhost ~]$


Oh, I see you have commented out your source the global bashrc in your ~/.bashrc. :A


Return to “Tips & Tricks”