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 ... 10, 11, 12 ... 14, 15, 16  Next  
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
loony
Apprentice
Apprentice


Joined: 25 Feb 2004
Posts: 199

PostPosted: Wed Nov 30, 2005 12:44 pm    Post subject: Shell tips Reply with quote

I couldn't find a similar thread so I am beginning a new one so we can collect some tips and tricks for the console users out there. I start with:

How to kill many processes at once

Code:
ps -A | grep process | awk '{print $1}' | kill -9


ps -A lists all processes, then with grep and the process name after it the list is shrinked to only the processes selected. awk gets only the first column ($1), finally the processes are killed with kill. I needed this for killing e.g. "kdesktop_lock" which slowed down my system with over 40 proceses of the same name. Be sure to kill the right process, test without kill command if you are unsure.

loony


Last edited by loony on Wed Nov 30, 2005 1:38 pm; edited 1 time in total
Back to top
View user's profile Send private message
Dizzutch
Guru
Guru


Joined: 09 Nov 2004
Posts: 463
Location: Worcester, MA

PostPosted: Wed Nov 30, 2005 1:16 pm    Post subject: Reply with quote

a sequence of BASH commands that I like to use is the following:

Code:
for i in *; do [ -d $i ] && du -sh $i; done


It gives a nice listing of all directories in the current directory, and their size in MB.
This way I can see exactly what directories are hogging disk space.
I'm sure there's many other ways to find this information, but I like this format.
-Dizz
_________________
Help the confused! Adopt an unanswered post!
prepend [solved] to your post title when you feel your issue is resolved.
Worcester Judo
Back to top
View user's profile Send private message
RiBBiT
Apprentice
Apprentice


Joined: 18 May 2005
Posts: 215
Location: Sweden

PostPosted: Wed Nov 30, 2005 3:17 pm    Post subject: Reply with quote

loony: You could take a look at the pgrep and pkill commands as well.

Dizzutch: Thank you, that is useful.
_________________
Comix - GTK Comic Book Viewer [ http://comix.sourceforge.net ]
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 18111

PostPosted: Wed Nov 30, 2005 5:47 pm    Post subject: Reply with quote

Dizzutch wrote:
a sequence of BASH commands that I like to use is the following:

Code:
for i in *; do [ -d $i ] && du -sh $i; done


It gives a nice listing of all directories in the current directory, and their size in MB.
This way I can see exactly what directories are hogging disk space.
I'm sure there's many other ways to find this information, but I like this format.
-Dizz
Just an FYI, it won't work with hidden directories, or directories with spaces (maybe some special characters too, I forget) in their names.

An alternative would be:
Code:
find -maxdepth 1 -type d -print0 | xargs -0 du -sh


EDIT: Put it in a script, and you can do "current directory" or specify a starting point.
Code:
#!/bin/bash

find ${1-.} -maxdepth 1 -type d -print0 | xargs -0 du -sh
Fixed based on ciaranm's comments.
Fixed to actually work with spaces.

_________________
Those who know what's best for us must rise and save us from ourselves.


Last edited by pjp on Wed Nov 30, 2005 7:55 pm; edited 2 times in total
Back to top
View user's profile Send private message
Dizzutch
Guru
Guru


Joined: 09 Nov 2004
Posts: 463
Location: Worcester, MA

PostPosted: Wed Nov 30, 2005 6:12 pm    Post subject: Reply with quote

thanks for the input pjp!
_________________
Help the confused! Adopt an unanswered post!
prepend [solved] to your post title when you feel your issue is resolved.
Worcester Judo
Back to top
View user's profile Send private message
ciaranm
Retired Dev
Retired Dev


Joined: 19 Jul 2003
Posts: 1719
Location: In Hiding

PostPosted: Wed Nov 30, 2005 6:37 pm    Post subject: Reply with quote

pjp wrote:
EDIT: Put it in a script, and you can do "current directory" or specify a starting point.
Code:
#!/bin/bash

if [ $1 ];
        then
                find $1 -type d -maxdepth 1 |xargs du -sh
        else
                find -type d -maxdepth 1 |xargs du -sh
fi

Unportable. find ${1-.} -maxdepth 1 -type d is the safe way.
Back to top
View user's profile Send private message
Earthwings
Bodhisattva
Bodhisattva


Joined: 14 Apr 2003
Posts: 7753
Location: Karlsruhe, Germany

PostPosted: Wed Nov 30, 2005 6:40 pm    Post subject: Reply with quote

Out of curiosity, which part of the script is not portable?
_________________
KDE
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 18111

PostPosted: Wed Nov 30, 2005 6:45 pm    Post subject: Reply with quote

Earthwings wrote:
Out of curiosity, which part of the script is not portable?
I think he's referring to $1 vs. ${1-.}. I've seen the {}, but never the -. part. (Surely having maxdepth before type isn't critical -- poorly designed programs if that makes a difference)
_________________
Those who know what's best for us must rise and save us from ourselves.
Back to top
View user's profile Send private message
ciaranm
Retired Dev
Retired Dev


Joined: 19 Jul 2003
Posts: 1719
Location: In Hiding

PostPosted: Wed Nov 30, 2005 6:46 pm    Post subject: Reply with quote

Earthwings wrote:
Out of curiosity, which part of the script is not portable?

The correct invocation for find is find paths (behaviour-switches)? (rules)? (actions)? . The paths part isn't optional, and the ordering for the different switch types is important.
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 18111

PostPosted: Wed Nov 30, 2005 6:52 pm    Post subject: Reply with quote

${1-.} is pretty cool. Updated the "script"
_________________
Those who know what's best for us must rise and save us from ourselves.
Back to top
View user's profile Send private message
Earthwings
Bodhisattva
Bodhisattva


Joined: 14 Apr 2003
Posts: 7753
Location: Karlsruhe, Germany

PostPosted: Wed Nov 30, 2005 6:55 pm    Post subject: Reply with quote

Ah, I see.

@pjp: ${1-.} is evaluated as $1 if it's set and a dot otherwise. I think that the pipe with xargs doesn't deal with spaces as well.
Code:
find ${1-.} -maxdepth 1 -type d -exec du -sh {} \;
should do that.
_________________
KDE
Back to top
View user's profile Send private message
ciaranm
Retired Dev
Retired Dev


Joined: 19 Jul 2003
Posts: 1719
Location: In Hiding

PostPosted: Wed Nov 30, 2005 6:57 pm    Post subject: Reply with quote

pjp wrote:
(Surely having maxdepth before type isn't critical -- poorly designed programs if that makes a difference)

If you're running ~arch with GNU findutils, sticking the -maxdepth in the wrong place will give you:
Code:

find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.

Future versions will make this a fatal error.
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 18111

PostPosted: Wed Nov 30, 2005 8:02 pm    Post subject: Reply with quote

Earthwings wrote:
I think that the pipe with xargs doesn't deal with spaces as well.
You are correct. Apparently "print0" and "-0" are needed for that. Fixed. Using xargs can improve performance for some actions.
pjp wrote:
Code:
find -maxdepth 1 -type d -print0 | xargs -0 du -sh


ciaranm wrote:
If you're running ~arch with GNU findutils, sticking the -maxdepth in the wrong place will give you:
<snip>
Future versions will make this a fatal error.
I guess I've never used complex enough find statements for it to matter. In these simple examples, "find directories no more than 1 level deep" doesn't seem different than "no more than 1 level deep, find directories." Not disagreeing with you, just explaining.

Having to do it "the right way" will probably just force me to remember more instead of doing 'man find' for more than the simplest statements.
_________________
Those who know what's best for us must rise and save us from ourselves.
Back to top
View user's profile Send private message
ciaranm
Retired Dev
Retired Dev


Joined: 19 Jul 2003
Posts: 1719
Location: In Hiding

PostPosted: Wed Nov 30, 2005 8:12 pm    Post subject: Reply with quote

pjp wrote:
I guess I've never used complex enough find statements for it to matter. In these simple examples, "find directories no more than 1 level deep" doesn't seem different than "no more than 1 level deep, find directories." Not disagreeing with you, just explaining.

The "find directories" or "named blah" type rules work at a "we've found an item, do we call the action for it?" level. The maxdepth thing affects the code which finds items. It basically makes sense once you read the POSIX description of how find is supposed to work -- there's separation between "finding stuff" and "deciding whether stuff is relevant".
Back to top
View user's profile Send private message
Omega21
l33t
l33t


Joined: 14 Feb 2004
Posts: 788
Location: Canada (brrr. Its cold up here)

PostPosted: Sun Dec 04, 2005 4:35 am    Post subject: Reply with quote

hardcore wrote:
steveb wrote:
this one starts manny bash prcesses and you can watch the cpu going crazy:
Code:
:(){ :|:&};:


cheers

SteveB


btw: don't do it if you are not fast enought to do an killall bash!


HaHaHa you *forking* bastard ;) That's a sure fire way to bring down any system quickly :lol:


Id love to cat urandom to a file and see how long it takes for the hard drive to fill up. :)
_________________
iMac G4 1GHz :: q6600 //2x 500GB//2GB RAM//8600GT//Gentoo :: MacBook Pro//2.53GHz
Back to top
View user's profile Send private message
SubAtomic
Apprentice
Apprentice


Joined: 20 Dec 2003
Posts: 255
Location: Hobart, TAS, Australia

PostPosted: Sun Dec 04, 2005 10:30 am    Post subject: Reply with quote

Not really a bash trick so to speak but this is the first thing I add to ~/.bashrc when ever I'm on a new machine ...
Code:
alias hpg="history | grep"


I use "history | grep" like its going out of style :)
_________________
"The real romance is out ahead and yet to come. The computer revolution hasn't started yet. Don't be misled by the enormous flow of money into bad defacto standards for unsophisticated buyers using poor adaptations of incomplete ideas." -- Alan Kay
Back to top
View user's profile Send private message
jomputin
n00b
n00b


Joined: 26 Nov 2005
Posts: 21
Location: Finland

PostPosted: Wed Jan 18, 2006 7:07 pm    Post subject: Reply with quote

Code:

alias pien=pine
alias pnei=pine
alias pnie=pine
alias pin=pine
alias ipen=pine
alias ipne=pine
alias pinr=pine
alias ine=pine


...and counting... :P
Back to top
View user's profile Send private message
h0mer`-
Apprentice
Apprentice


Joined: 02 Aug 2004
Posts: 215

PostPosted: Tue Jun 20, 2006 10:40 am    Post subject: Reply with quote

hey there, interesting thread....

is there any possibility to have the same local bash prompt when i log in through ssh without manually sourcing the /etc/bashrc ...
Back to top
View user's profile Send private message
Naib
Watchman
Watchman


Joined: 21 May 2004
Posts: 5702
Location: Removed by Neddy

PostPosted: Tue Jun 20, 2006 11:27 am    Post subject: Reply with quote

h0mer`- wrote:
hey there, interesting thread....

is there any possibility to have the same local bash prompt when i log in through ssh without manually sourcing the /etc/bashrc ...


you should just be able to make a .bash_profile

Code:

//LKGD718D2~# cat .bash_profile
[ -f .bashrc ] && source .bashrc

_________________
The best argument against democracy is a five-minute conversation with the average voter
Great Britain is a republic, with a hereditary president, while the United States is a monarchy with an elective king
Back to top
View user's profile Send private message
nephros
Advocate
Advocate


Joined: 07 Feb 2003
Posts: 2139
Location: Graz, Austria (Europe - no kangaroos.)

PostPosted: Tue Jun 20, 2006 4:17 pm    Post subject: Reply with quote

So if we're nitpicking...
ciaranm wrote:
pjp wrote:
EDIT: Put it in a script, and you can do "current directory" or specify a starting point.
Code:
#!/bin/bash

if [ $1 ];
        then
                find $1 -type d -maxdepth 1 |xargs du -sh
        else
                find -type d -maxdepth 1 |xargs du -sh
fi

Unportable. find ${1-.} -maxdepth 1 -type d is the safe way.

... $1 should be quoted.

1) to make it robust against arguments starting with a dash.
2) so no one can sneak in malicious values for $1 (tricky but doable).
_________________
Please put [SOLVED] in your topic if you are a moron.
Back to top
View user's profile Send private message
xPAGANx
Tux's lil' helper
Tux's lil' helper


Joined: 08 Oct 2003
Posts: 93
Location: Warren, Michigan

PostPosted: Tue Jun 20, 2006 4:38 pm    Post subject: Reply with quote

If you run behind a proxy, adding
Code:
export http_proxy="http://yourproxy:port"

to your bashrc saves a lot of typing.
Back to top
View user's profile Send private message
DaFrEQ
Apprentice
Apprentice


Joined: 07 Apr 2004
Posts: 218
Location: Earth... for now

PostPosted: Tue Jun 20, 2006 8:29 pm    Post subject: Reply with quote

Question regarding the bash prompt customization.
I'm currently using:
Code:
PS1='\[\033[0;31m╓\[\033[0;32m[\[\033[1;30m\d - \t\[\033[0;32m]\n\[\033[0;31m╙\[\033[0;32m[\[\033[0;33m\u\[\033[1;30m@\[\033[0;34m\h\[\033[0;32m]\[\033[1;30m->\[\033[0;35m '
Using this for my regular user.
However, I've noticed that it works fine in xterm, but the extended ascii characters at the beginning don't display correctly in my aterm or a regular command prompt (i.e. Ctrl+Alt+F1)

I was wondering if there was a way to:
A) Get aterm to display the extended ascii chars
or
B) Edit the fluxbox menu so that my aterm will have a different prompt than my xterm?
(I use both for different things, so a different prompt would probably suit me better, but I'm not sure if this can all be done in my fluxbox menu file)

Any ideas/help is greatly appreciated.

Tnx
L8rz
_________________
--== Linux: More configurable than a Mr. Potato Head ==--
Distros: SusE10.1(amd64); Gentoo 2006.1; Slack9; LFS3; Solaris8(sparc)
Back to top
View user's profile Send private message
cybrstuff
n00b
n00b


Joined: 11 Jun 2006
Posts: 2
Location: Marlborough, MA

PostPosted: Wed Jun 21, 2006 4:20 am    Post subject: Reply with quote

Here are mine:
Code:

export GREP_COLOR="01;33"
export GREP_OPTIONS="-i"
alias grep="grep --color=always"
alias ls="ls --color=always -h"
alias ll="ls -l"
alias xx="cd .."
alias lg="ls -l | grep "
export LESS="-iR"

I'm particularly fond of the lg one for finding file information. Also, the grep colors are sexy, and the --color=always with the less -iR combo means that when you do an 'll | less', you can still see the colors for directories, etc.

I'm also very fond of my prompt:
Code:

function prompt_command {

local host=`echo $HOSTNAME | sed "s/\..*//"`
local temp="[$USER@$host]|[$PWD]|[$(date +%I:%M)]"
let fill=$(( $COLUMNS-${#temp} ))

if [ "$fill" -ge "0" ]
then
        local junk="                                                                                                                                                                                                                                                                                                                               "
        junk="${junk:0:${fill}}"
        local newPWD="$PWD"
else
        local junk=""
        let cut=3-${fill}
        local newPWD="...${PWD:${cut}}"
fi

if [ "$TERM" = "xterm" ]
then
        local title="\[\033]0;\u@\h: \w\007\]"
else
        local title=""
fi

if [ "$EUID" = "0" ]
then
        local color="1;31m"
else
        local color="1;32m"
fi

PS1="$title\
[\[\033[$color\]\u@\h\[\033[0m\]]\
|[\[\033[1;34m\]$newPWD$junk\[\033[0m\]]\
|[\[\033[1;36m\]$(date +%I:%M)\[\033[0m\]]\n\
-=- "
}

PS2="\[\033[0;37m\]-=-\[\033[0m\] "

PROMPT_COMMAND='prompt_command;echo -n -e "\033k\033\134"'

unset fill cut

It has a couple of neat features. If the working directory is too long for the screen, it truncates the first bit to keep it to one line. Also, the color of the username@machine is green if you're a normal user and red if you're root.

Enjoy!
Back to top
View user's profile Send private message
R.I.P.
Tux's lil' helper
Tux's lil' helper


Joined: 02 May 2006
Posts: 90
Location: Kiev, Ukraine

PostPosted: Wed Jun 21, 2006 11:15 pm    Post subject: Reply with quote

here is my simple alarm clock
plays random mp3 from a list in file ~/.mycfg/.alarm-sounds at specified time
Code:

#!/bin/bash

SOUNDS=~/.mycfg/.alarm-sounds
SOUNDIDX=($RANDOM%`grep '.mp3\s*$' $SOUNDS| wc -l`+1)
SOUND=`grep '.mp3\s*$' $SOUNDS| sed "$SOUNDIDX!d"`
echo mpg123 \"$SOUND\" | at $1


could be used like:
alarm 7:30
Back to top
View user's profile Send private message
Netfeed
n00b
n00b


Joined: 24 Jan 2004
Posts: 19

PostPosted: Wed Aug 02, 2006 2:26 pm    Post subject: The .bashrc thread Reply with quote

Code:

alias .='echo -e $PWD'
#-> Example: $ /home/netfeed/public_html
alias ..="cd .."
alias ...="cd ../.."

alias bittornado="btlaunchmanycurses.py  --random_port 1  --auto_kick 1 --minport 30900 --maxport 31000 --security 1"
alias c="clear"
alias color="./scripts/color.sh"
alias df="df -kTh"
alias grep="grep --color=auto"
alias k="kill -9"
alias less="less -R"
alias movie="~/scripts/movie.sh"
alias nano="nano -w"
alias o='echo -e $OLDPWD'
#-> echos the previous path
alias p="ps aux | grep"
alias pine="pine -i"
alias rflux="kill -s usr2 `pgrep fluxbox`"
#-> reload the fluxbox config
alias root="su -"
alias s="screen -dr"
#-> open and, if needed, reattaches the choosed screen

# -- irssi
alias quakenet="irssi -c quakenet -n Netfeed"
alias dormnet="irssi -c dormnet"

# -- ls family
alias ls="ls -hF --color" # add colors for filetype recognition
alias ll="ls -l"          # show file list
alias la="ls -Al"         # show hidden files
alias lx="ls -lXB"        # sort by extension
alias lk="ls -lSr"        # sort by size
alias lc="ls -lcr"        # sort by change time 
alias lu="ls -lur"        # sort by access time   
alias lr="ls -lR"         # recursive ls
alias lt="ls -ltr"        # sort by date
alias lm="ls -al | less"  # pipe through 'less'

# -- function for commands with colors see alias color for options
# -+ \[\033[1;32m\] to only  set the fg, this gives green
# -+ \[\033[44;1;32m\] to set fg and bg, this gives green fg on blue bg
function colors {
local CLEAR="\[\033[0m\]"
local RED="\[\033[1;31m\]"
local GREEN="\[\033[1;32m\]"
local BLUE="\[\033[1;34m\]"
local YELLOW="\[\033[1;33m\]"

if [ "$EUID" = "0" ] || [ "$USER" = "root" ]; then
        PS1="$RED\u$CLEAR@$BLUE\h$CLEAR[\W]: "
else
        PS1="$GREEN\u$CLEAR@$BLUE\h$CLEAR[\W]: "
fi

#-> Example: netfeed@picard[~]:
PS2="$YELLOW>$CLEAR "
#-> Example: $ echo hello, \
#-> Example: > nice to meat you
PS4="$0 line $LINENO: "
#-> used wit "set -o xtrace" in bash
#-> Example: $ movie line 3: STDERR;
}
colors
#-> call the function, needs to be a function to be able to
#-> use local variables.

if [ "$EUID" = "0" ] || [ "$USER" = "root" ] ; then
        PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${ROOTPATH}"
else
        PATH="/usr/local/bin:/usr/bin:/bin:${PATH}"
fi
export PATH
unset ROOTPATH

CDPATH=:~/public_html/skola:

# -- change the window title of X terminals
# -+ if you run a terminal localy, the title would be user@foo,
# -+ if you then ssh to server and the server has the same code then
# -+ the title of the terminal will be user@bar
case $TERM in
        xterm*|rxvt)
               PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\007"'  ;;
esac

[ -f /etc/profile.d/bash-completion ] && source /etc/profile.d/bash-completion

_________________
tie me up, spank me hard and call me virgin mary
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 ... 10, 11, 12 ... 14, 15, 16  Next
Page 11 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