Command Line Program Examples

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

I was thinking it might be a good idea to have a resource in the Unix/Linux forum where people can post how to use command line programs. More so interesting ways to use them, or just things you can do with them that the beginning Linux user might not know and maybe looking for.

For example:

You can run the following command to see memory usage of currently running process for a specific user.

Code: [ Select ]
ps -u root -o rss,command


You can add the following to remove certain commands with the given phrase in the command.

Code: [ Select ]
grep -v string


You can also add the following to sum the first column and return the results, in this case its the memory used and it returns the value in MB.

Code: [ Select ]
awk '{sum+=$1} END {print sum/1024}'


All elements together:
Code: [ Select ]
ps -u root -o rss,command | grep -v string | awk '{sum+=$1} END {print sum/1024}'


Do you think this would be a good resource to add to the forum?

I wouldn't expect this to be a sub category, more of just an organized thread. I just wanted to get some feedback on the idea.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9099
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

I think its a good idea and something that could be useful. I use the command line all the time to run and administer linux servers for websites which include ozzu. Some of the most important things I do involve using the command line and pico (not into vi).
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

NOT into vi?! :shock: j/k. :) I do like the idea. Perhaps I can throw in a few pointers here and there as well. All of our VM servers @ work are linux, so I'm in the same boat.
  • kc0tma
  • o|||||||o
  • Web Master
  • User avatar
  • Posts: 3318
  • Loc: Trout Creek, MT

Post 3+ Months Ago

Code: [ Select ]
who | sort | uniq | wc -l


...will tell you how many users are logged onto a system. I put that into a little shell script then put that script in /usr/local/bin so I can call it up faster than you can snap your fingers.
  • jitendraag
  • Newbie
  • Newbie
  • jitendraag
  • Posts: 8

Post 3+ Months Ago

The most used for me would be tar -xvzf filename.tar ;-)
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

whoami is nice for determining the user a script interpreter or similar application able to pass commands to a shell is logged in as.

whereis is nice for determining where an application is.
Code: [ Select ]
joebert@home:~$ whereis php
php: /usr/bin/php /usr/share/man/man1/php.1.gz
  1. joebert@home:~$ whereis php
  2. php: /usr/bin/php /usr/share/man/man1/php.1.gz
  • middayc
  • Novice
  • Novice
  • User avatar
  • Posts: 23
  • Loc: Slovenia

Post 3+ Months Ago

nice topic (I didn't know for that usage of ps, and will need it)..
I collect linux commandline tricks that I need and find here and there on some wiki page because otherwise I would always forget them when I needed them. Here are few more interesting ones:

Count files with <error> in them:
Code: [ Select ]
grep -L "<error>" * | wc -l


Remove files that don't have this string in them
Code: [ Select ]
grep -L --directories=skip "<error>" * | xargs rm


Detach script from your shell session (& does that) so you can run other apps and so that it keeps executing after you close the ssh console (nohup does that):
Quote:
nohup wget http://www.co.com/some-big-file.zip &
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6254
  • Loc: Seattle, WA

Post 3+ Months Ago

Code: [ Select ]
fortune
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

A majority of these are probably linux specific. That's all I really use, so it's what I know. ;) I didn't list all the flags associated with each command, but I tried to include the useful ones, which brings us to the first in my list:

man - the built-in help manual. Specify a command or app that you want info on. Format: man grep

ls - List contents of current directory (useful flags: -l, -a). Format: ls -a

df - Show used disk space and mounted drives (default returns size in blocks, use -h flag to specify common units). Format: df -h

locate - search utility. Keeps a db listing of files - generally faster returns than find. Format: locate [filename]

find - The locate db may not always be current enough to find what you need. In that case, use find. Format: find [path] -name [filename]

cat - displays the entire contents of a file. Not very useful for large files. Format: cat [filename]

head - view the top portion of a file. (-n flag sets the number of lines to view - default is 10 lines). Format: head -60 [filename]

tail - reverse of head - view the end of a file. (-n flag is the same, -f will follow a file live as it changes). Format: tail -n 60 -f

sudo - used when superuser rights are required for a command. Format: sudo tail -n 60 [filename]

ifconfig - Display the IP's that the system is using and the amount of traffic sent/received (-a flag to view all). Format: ifconfig eth0

top - Graphical real time process monitor. Format: top

ps - Overview of all running processes. Format: ps

kill - Kill a process (use top or ps to identify the PID and then plug that in). Format: kill -9 or killall -9

cp - Copy a file. Format: cp [orig filename] [destination path]

mv - Move a file. Format: mv [orig filename] [destination path]

scp - securely copy file over ssh. Format: scp [filename] [user]@[remote host]:/[path to file]

shutdown - Shut down the system (-r flag to set delay to 'now' or a number of minutes). Format: shutdown -r now

I've excluded grep from my list since it's been mentioned a few times, but I highly recommend that anyone using command line take a hard look at grep and play around with it until you're familiar with its basic capabilities. Entire books can (and have!) been written about grep. It's a powerful tool!
  • kapalpecah
  • Newbie
  • Newbie
  • kapalpecah
  • Posts: 13

Post 3+ Months Ago

change string ocurance in all files

Code: [ Select ]
find . | xargs perl -p -i.old -e 's/needreplace/newstring/g'
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

Count the lines in a directory full of files, listing per-file and total results.

Code: [ Select ]
me@here:/lists$ ls
2009-07-22  2009-07-23  2009-07-24
 
me@here:/lists$ wc -l *
  109 2009-07-22
  739 2009-07-23
   12 2009-07-24
  860 total
  1. me@here:/lists$ ls
  2. 2009-07-22  2009-07-23  2009-07-24
  3.  
  4. me@here:/lists$ wc -l *
  5.   109 2009-07-22
  6.   739 2009-07-23
  7.    12 2009-07-24
  8.   860 total


Count the unique lines in a file

Code: [ Select ]
me@here:/lists$ cat list.txt | uniq | wc -l
1490
  1. me@here:/lists$ cat list.txt | uniq | wc -l
  2. 1490
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

If you use RSA keys to login via SSH instead of being prompted for your password, you can use tab-completion while typing a remote file path for SCP.

Code: [ Select ]
scp me@remote:/complete-using-tab ...
  • hostechsupport
  • Newbie
  • Newbie
  • hostechsupport
  • Posts: 13

Post 3+ Months Ago

If you want to delete files older than certain number of days then you can use the following command :-


BASH Code: [ Select ]
find /path/to/files* -mtime +10 -exec rm {} \;


where -mtime is used to specify the number of days old that the file is. If you enter +10 as shown in the command , it will find files older than 10 days.

And -exec, allows you to pass in a command such as rm(remove). The {} \; at the end is required to end the command.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

The -atime flag can be substituted for the -mtime flag there to delete files that haven't been accessed in N days. Works great for cache folders.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9099
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

joebert wrote:
whereis is nice for determining where an application is.
Code: [ Select ]
joebert@home:~$ whereis php
php: /usr/bin/php /usr/share/man/man1/php.1.gz
  1. joebert@home:~$ whereis php
  2. php: /usr/bin/php /usr/share/man/man1/php.1.gz


Interesting never used that one before. I always use which:

Code: [ Select ]
[0.26] root@server1 [~]# which php
/usr/local/bin/php
  1. [0.26] root@server1 [~]# which php
  2. /usr/local/bin/php
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9099
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

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 :)
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

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
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9099
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

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
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

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:
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9099
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

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.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

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.
  • akpk
  • Banned
  • Banned
  • User avatar
  • Posts: 3

Post 3+ Months Ago

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

Code: [ Select ]
find ./ -type d -size +1G
  • lance5434
  • Born
  • Born
  • lance5434
  • Posts: 2

Post 3+ Months Ago

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
  • lance5434
  • Posts: 2

Post 3+ Months Ago

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
  • Posts: 8
  • Loc: Malaysia

Post 3+ Months Ago

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 10 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.