Command Line Program Examples

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Joined: Dec 20, 2002
  • Posts: 8924
  • Loc: Seattle, WA & Phoenix, AZ
  • Status: Offline

Post January 30th, 2010, 5:19 pm

Another thing I do which for me is quite useful is make so that the load is shown every time I execute a command or even by just pressing the enter key. It is part of my prompt. I also have it set so that depending on what the load is different colors are enabled, or if really bad it will flash at me and warn me that I should be trying to fix it. When loads are good its a greyish color, and as they get worse they get more an more noticeable (such as red or purple flashing). Before when I would check loads I would simply type "uptime" as that shows the loads as well.

Here is what my .bashrc looks like:

Code: [ Select ]
# .bashrc

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

# User specific aliases and functions


function load_out() {
 echo -n "$(uptime | sed -e "s/.*load average: \(.*\...\), \(.*\...\), \(.*\...\).*/\1/" -e "s/ //g")"
}

function load_color() {
 tmp=$(echo $(load_out)*100 | bc)
 let load100=${tmp%.*}

 if [ ${load100} -lt 100 ]
 then
    echo -n "1;30"
 elif [ ${load100} -ge 100 ] && [ ${load100} -lt 150 ]
 then
    echo -n "0;30"
 elif [ ${load100} -ge 150 ] && [ ${load100} -lt 200 ]
 then
    echo -n "0;33"
 elif [ ${load100} -ge 200 ] && [ ${load100} -lt 300 ]
 then
    echo -n "1;31"
 elif [ ${load100} -ge 300 ] && [ ${load100} -lt 500 ]
 then
    echo -n "1;35"
 else
    echo -n "5;1;35"
 fi
}

function load {
    PS1="[\[\e[\$(load_color)m\]\$(load_out)\[\e[m\]] \u@\h [\w]\$ "
}

PROMPT_COMMAND="load"
  1. # .bashrc
  2. # Source global definitions
  3. if [ -f /etc/bashrc ]; then
  4.     . /etc/bashrc
  5. fi
  6. # User specific aliases and functions
  7. function load_out() {
  8.  echo -n "$(uptime | sed -e "s/.*load average: \(.*\...\), \(.*\...\), \(.*\...\).*/\1/" -e "s/ //g")"
  9. }
  10. function load_color() {
  11.  tmp=$(echo $(load_out)*100 | bc)
  12.  let load100=${tmp%.*}
  13.  if [ ${load100} -lt 100 ]
  14.  then
  15.     echo -n "1;30"
  16.  elif [ ${load100} -ge 100 ] && [ ${load100} -lt 150 ]
  17.  then
  18.     echo -n "0;30"
  19.  elif [ ${load100} -ge 150 ] && [ ${load100} -lt 200 ]
  20.  then
  21.     echo -n "0;33"
  22.  elif [ ${load100} -ge 200 ] && [ ${load100} -lt 300 ]
  23.  then
  24.     echo -n "1;31"
  25.  elif [ ${load100} -ge 300 ] && [ ${load100} -lt 500 ]
  26.  then
  27.     echo -n "1;35"
  28.  else
  29.     echo -n "5;1;35"
  30.  fi
  31. }
  32. function load {
  33.     PS1="[\[\e[\$(load_color)m\]\$(load_out)\[\e[m\]] \u@\h [\w]\$ "
  34. }
  35. PROMPT_COMMAND="load"


So with using that I am always informed of the load by just being logged in. So my prompt would look something like this:

Code: [ Select ]
[0.31] root@server1 [/home/admin]#


Notice the load at the beginning :)
Ozzu Hosting - Want your website on a fast server like Ozzu?
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post January 30th, 2010, 5:19 pm

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post January 30th, 2010, 6:10 pm

Sometimes I just need to do a quick resize of an image for posting on a forum or something. It's a lot easier to do it from a terminal than it is to fire up Fireworks/Photoshop in Wine.

The -resize switch basically works like "WxH", will accept pixel counts or percentages, and will automatically determine a missing dimension using the images aspect ratio.

After that I just need the source images filename, and a destination image filename. It's smart enough to tell I want to convert from PNG to JPG in the following example as well.

Code: [ Select ]
joebert@home:~/$ convert -resize 50%x img.png img.jpg
Strong with this one, the sudo is.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Joined: Dec 20, 2002
  • Posts: 8924
  • Loc: Seattle, WA & Phoenix, AZ
  • Status: Offline

Post February 5th, 2010, 5:07 pm

Somtimes I realize that a certain website is using alot of space. The problem is many of these websites have numerous folders so its hard to pinpoint. With this command you can list all of the diretories and files in current path/directory and how much space is being used by each one. It is very helpful in finding files that are eating up all of the hard drive space:

Code: [ Select ]
du --max-depth=1 | sort -n -r


That code also sorts it in order from the directory or file using the most to the least. The output will look something like:

Code: [ Select ]
[1.35] root@server1 [/home/user]# du --max-depth=1 | sort -n -r
55579028    .
53789164    ./public_html
1317184 ./tmp
200   ./mail
172   ./.cpanel
  1. [1.35] root@server1 [/home/user]# du --max-depth=1 | sort -n -r
  2. 55579028    .
  3. 53789164    ./public_html
  4. 1317184 ./tmp
  5. 200   ./mail
  6. 172   ./.cpanel
Ozzu Hosting - Want your website on a fast server like Ozzu?
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post February 5th, 2010, 5:26 pm

I like to use the -h switch with du, partly because it produces "human readable" numbers but mainly because "du -h" or duh is easy for me to remember.

I suppose it would interfere with the sorting though. :scratchhead:
Strong with this one, the sudo is.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Joined: Dec 20, 2002
  • Posts: 8924
  • Loc: Seattle, WA & Phoenix, AZ
  • Status: Offline

Post February 5th, 2010, 10:33 pm

Yup just tried it, main issue with that is the sorting problem because of the different units used. Definetely is more readable, but its nice to just look at the top and see which directory is eating the most hard drive space.
Ozzu Hosting - Want your website on a fast server like Ozzu?
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post February 6th, 2010, 5:14 pm

I've never had a need to do this but I see a lot of people asking about it around the net only to get complicated answers.

Assuming you have a directory full of files, and you want to delete all files except for the newest 5, you can do this.

BASH Code: [ Select ]
rm `ls -tr | head -n '-5'`


If there are less than or equal to 5 files rm will error out and nothing will be deleted.
Strong with this one, the sudo is.
  • akpk
  • Banned
  • Banned
  • User avatar
  • Joined: Aug 26, 2010
  • Posts: 3
  • Status: Offline

Post August 27th, 2010, 12:31 am

I would use this command to find the files which are larger in size.

Code: [ Select ]
find ./ -type d -size +1G
  • lance5434
  • Born
  • Born
  • No Avatar
  • Joined: Mar 21, 2011
  • Posts: 2
  • Status: Offline

Post March 21st, 2011, 3:20 pm

free -m

Gives space on computer. free -g is in gigabytes and free -m is in megabytes. free -m is most useful.
  • lance5434
  • Born
  • Born
  • No Avatar
  • Joined: Mar 21, 2011
  • Posts: 2
  • Status: Offline

Post March 22nd, 2011, 1:44 pm

When using "sudo", you will need to type in sudo password.
However, you shouldn`t be using sudo too much.
  • weldan
  • Newbie
  • Newbie
  • User avatar
  • Joined: Aug 16, 2012
  • Posts: 8
  • Loc: Malaysia
  • Status: Offline

Post August 16th, 2012, 12:46 pm

netstat

to show opened port and connection.

$ netstat -at

etc

Post Information

  • Total Posts in this topic: 25 posts
  • Users browsing this forum: No registered users and 113 guests
  • 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 post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.