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

Re: Making your own commands!

Postby viking60 » 09 Nov 2011, 20:34

AH OK i will jump in there and have a look. Menawhile a have found the ultimate ls alias you have got to use this one - no discussion do it! :T

Code: Select all

 alias ls='ls -u --color=auto --classify --time-style=long-iso --group-directories-first --si --quoting-style=shell'

It organizes the directories first and gives you colors and.... it rules :s
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
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Making your own commands!

Postby viking60 » 09 Nov 2011, 20:54

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


Yes thanks! Reactivating it had no negative impact - and I still have my smiley bash prompt :greetings
:( cat .bashrc
# .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'
alias ls='ls -u --color=auto --classify --time-style=long-iso --group-directories-first --si --quoting-style=shell'

#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)
smileyc=$(smileyc $RTN)
local CY="\[\e[1;31m\]" # Each is 12 chars long
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
.:[ thomas@thomas-pc : 20:53:53 : ~ ]:.
:)

From the /etc/profile.d/60alias.sh file:
# 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.

Uh where?
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
dedanna1029
Sound-Berserk
Posts: 8784
Joined: 14 Mar 2010, 20:29
Contact:

Re: Making your own commands!

Postby dedanna1029 » 09 Nov 2011, 21:23

I'd rather be a free person who fears terrorists, than be a "safe" person who fears the government.
No gods, no masters.
"A druid is by nature anarchistic, that is, submits to no one."
http://uk.druidcollege.org/faqs.html

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

Re: Making your own commands!

Postby viking60 » 09 Nov 2011, 21:32

Yes I know about .bashrc (that works just fine!) but is should be put in a separate file in Mandriva apparently. I tried to make ~/.alias but no luck :confused
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
dedanna1029
Sound-Berserk
Posts: 8784
Joined: 14 Mar 2010, 20:29
Contact:

Re: Making your own commands!

Postby dedanna1029 » 09 Nov 2011, 21:38

Not finding anything at the MDV forum (or in a google specified search for mandriva), either on that. If the ~/.bashrc works, I'd just use that anyway. Taking a
Edit: How to create aliases in .bashrc - Mandriva Expert. The problem is, it's very outdated.
I'd rather be a free person who fears terrorists, than be a "safe" person who fears the government.
No gods, no masters.
"A druid is by nature anarchistic, that is, submits to no one."
http://uk.druidcollege.org/faqs.html

User avatar
dedanna1029
Sound-Berserk
Posts: 8784
Joined: 14 Mar 2010, 20:29
Contact:

Re: Making your own commands!

Postby dedanna1029 » 09 Nov 2011, 21:49

Should've known, my old trusty site.
Make an alias permanent

Use your favorite text editor to create a file called ~/.bash_aliases, and type the alias commands into the file.
.bash_aliases will run at login (or you can just execute it with ..bash_aliases )


Try ~/.bash_aliases
I'd rather be a free person who fears terrorists, than be a "safe" person who fears the government.
No gods, no masters.
"A druid is by nature anarchistic, that is, submits to no one."
http://uk.druidcollege.org/faqs.html

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

Re: Making your own commands!

Postby viking60 » 09 Nov 2011, 21:56

Will do! Gimme a minute......
No that was not it, but maybe .bash_profile......
No I give up and stick to .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
dedanna1029
Sound-Berserk
Posts: 8784
Joined: 14 Mar 2010, 20:29
Contact:

Re: Making your own commands!

Postby dedanna1029 » 09 Nov 2011, 23:15

Hey, if it works, it works.
I'd rather be a free person who fears terrorists, than be a "safe" person who fears the government.
No gods, no masters.
"A druid is by nature anarchistic, that is, submits to no one."
http://uk.druidcollege.org/faqs.html

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

Re: Making your own commands!

Postby viking60 » 10 Nov 2011, 20:02

Berserks don't give up so I gave it another go. I want to put all my aliases in one file. For several reasons:

1 I could not remember on what boxes I had implemented what aliases so I end up writing a lot of non-working commands
2 I don't remember (- but it was good so help me out here)

So I went to .bashrc and added these lines to all my boxes:

Code: Select all

if [ -f .alias ]; then
    . .alias
fi


Then I created the .alias file in my home directory:
.alias:

Code: Select all

alias cd..='cd ..';
alias df='df -h -x supermount';
alias ls='ls -u --color=auto --classify --time-style=long-iso --group-directories-first --si --quoting-style=shell';
alias dir='ls -u --color=auto --classify --time-style=long-iso --group-directories-first --si --quoting-style=shell'
alias copy='cp -i'
alias move='mv -i'
alias cd..='cd ..'
alias orphans='pacman-color -Qtdq'
alias removeorphans='sudo pacman -Rs $(pacman -Qqdt)'
alias ping='ping -c 10'
alias lsd='/bin/ls -FAv |grep /$ | column'


Finally I "restarted" my bash like this:

Code: Select all

source ~/.bashrc

Tested out the dir command and....
It works! :dance1

Now my .bashrc is much less cluttered - hmm I wonder if that was reason No. 2 :think: Anyway I can copy the alias file to all my boxes and have the same set of commands on them (I wonder if that was reason No. 3 :think: )
Distro spesific aliases can be added in .bashrc and the rest is only a matter of copying the .alias file to all computers.
All aliases can be added as a line in the file ~/.alias.
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
dedanna1029
Sound-Berserk
Posts: 8784
Joined: 14 Mar 2010, 20:29
Contact:

Re: Making your own commands!

Postby dedanna1029 » 12 Nov 2011, 22:42

Very nice. :)
I'd rather be a free person who fears terrorists, than be a "safe" person who fears the government.
No gods, no masters.
"A druid is by nature anarchistic, that is, submits to no one."
http://uk.druidcollege.org/faqs.html

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

Making ifconfig command in Mandriva

Postby viking60 » 18 Nov 2011, 12:47

It has annoyed me somewhat that there is no ifconfig command in Mandriva.
(it has annoyed others too)
You have to:

Code: Select all

cd /sbin

and then

Code: Select all

./ifconfig


So I needed to put this in an alias so that I can run ifconfig from the CLI like I can in Arch and demonstrate the combination of several aliases into one alias at the same time.

To do this I made three aliases:

Code: Select all

alias cdsbin='cd /sbin'
alias ifc='./ifconfig'
alias ifconfig='cdsbin; ifc; cd' 


As you may notice I have combined three aliases in that last alias.
cd /sbin jumps into the /sbin directory where we need to be in order to run ./ifconfig and the cd returns us back home after the command is executed.
if I only were to use:

Code: Select all

alias ifconfig='cdsbin; ifc;'

I would remain in the /sbin directory - but we want to get back to our /home so I added the cd

I added the three lines above to my ~/.alias file so it will remain on my computer.
Now I can enter ifconfig in the Mandriva Terminal and get the same results as in Arch:

Code: Select all

.:[ thomas@thomas-pc : 12:26:09 : ~ ]:.
:) ifconfig
eth0      Link encap:Ethernet  HWaddr 00:26:18:A8:F8:BC 
          inet addr:10.0.0.9  Bcast:10.0.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:562485 errors:0 dropped:0 overruns:0 frame:0
          TX packets:408474 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:236317722 (225.3 MiB)  TX bytes:59424475 (56.6 MiB)
          Interrupt:18

eth1      Link encap:Ethernet  HWaddr 00:26:18:A8:F8:BB 
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
          Interrupt:17

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:6091 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6091 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:618790 (604.2 KiB)  TX bytes:618790 (604.2 KiB)

There is probably an easier way of doing this. I will leave that up to our Gurus?
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 » 18 Nov 2011, 13:38

I've found that some files are in root's $PATH, not in normal user's, but don't require root privilege in order to be executed. For those commands, you can execute them as normal user by giving the full path. :A

Code: Select all

[rolf@localhost ~]$ echo $PATH
/usr/bin:/bin:/usr/local/bin:/usr/X11R6/bin/:/usr/games:/usr/lib/qt4/bin
[rolf@localhost ~]$ su
Password:
[root@localhost rolf]# echo $PATH
/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin:/usr/lib/qt4/bin
[root@localhost rolf]# exit
exit
[rolf@localhost ~]$ ifconfig
bash: ifconfig: command not found
[rolf@localhost ~]$ /sbin/ifconfig
eth0      Link encap:Ethernet  HWaddr 00:22:15:48:C7:E5 
          inet addr:192.168.1.103  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:13858 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10238 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:13474360 (12.8 MiB)  TX bytes:1598813 (1.5 MiB)
          Interrupt:18

eth1      Link encap:Ethernet  HWaddr 00:22:15:45:DB:98 
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
          Interrupt:17

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:82 errors:0 dropped:0 overruns:0 frame:0
          TX packets:82 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:4606 (4.4 KiB)  TX bytes:4606 (4.4 KiB)


So, if I follow you correctly, you could simplify your alias to just give the full path. Alternately, you could modify the user's $PATH to include these system administration types of directories. I'll leave doing that as an exercise for the padiberserks. :berserk2


Return to “Tips & Tricks”