Linux.com

Feature

CLI Magic: Information, please

By Joe Barr on July 25, 2005 (8:00:00 AM)

Share    Print    Comments   

There are lots of ways the CLI can help you. Often, you don't need to do something so much as you need to know something. This week we'll take a quick look at several commands -- whereis, whatis, df, kernelversion, ifconfig, route, and last -- that can quickly answer questions about your system or configuration. Wipe that GUI mess out of your eyes and meet me at the CLI.
Where is that darn command?

You know the command is on your system, because you've used it before. But now you can't remember where it lives. Is it in /sbin? Or /usr/bin? Somewhere else? The whereis command is designed to answer that very question. In fact, whereis will tell you not only where to find the executable command, but the source code -- if it's present -- and man pages as well. All you need to do is ask, by providing the name of the command you're looking for:

warthawg@linux:~> whereis whatis
whatis: /usr/bin/whatis /usr/share/man/man1/whatis.1.gz

What is that command for?

Maybe you've heard people mention a command, but aren't sure what it does. All you have to is ask. Here's a metaphysical example to consider:

warthawg@linux:~> whatis time
time (1)             - time a simple command or give resource usage
time (2)             - get time in seconds
time (n)             - Time the execution of a script
time (1p)            - time a simple command
time (3p)            - get time

How much disk space do I have left?

An excellent question to ask from time to time, and especially before a critical drive fills up completely. To get the answer, type df on the command line. This handy little utility shows you how much space you've used and how much is left in every mounted file system. Like this:

warthawg@linux:~> df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/hdc1             19550436   3493984  16056452  18%
tmpfs                   517572         0    517572   0% /dev/shm
/dev/hda2              7847500     32840   7814660   1% /downloads
/dev/hdd1            115380192  39944740  69574416  37% /home

I'm running the 2.4 kernel, what about you?

If you hang around Linux people very long, whether it's a LUG meeting or online chat, someone is going to ask what version of the kernel you're running. There's a command to tell, just in case you forget:

warthawg@linux:~> /sbin/kernelversion
2.6

What's my IP address?

Sometimes you just have to know your IP address. Entering ifconfig without any arguments is one way to find out, and learn a lot more as well:

warthawg@linux:~> /sbin/ifconfig
eth0      Link encap:Ethernet  HWaddr 00:50:8D:4E:9D:D0
          inet addr:192.168.0.101  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::250:8dff:fe4e:9dd0/64 Scope:Link
          UP BROADCAST NOTRAILERS RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:74969 errors:0 dropped:0 overruns:0 frame:0
          TX packets:76379 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:54194289 (51.6 Mb)  TX bytes:12136154 (11.5 Mb)
          Interrupt:11 Base address:0xe800

How do I get to the Internet from here?

OK, you found out what your IP address is -- even if it is private, like the one above -- but how do you talk to the Internet? The route command can give you the answer. No arguments required:

warthawg@linux:~> /sbin/route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     *               255.255.255.0   U     0      0        0 eth0
link-local      *               255.255.0.0     U     0      0        0 eth0
loopback        *               255.0.0.0       U     0      0        0 lo
default         192.168.0.1     0.0.0.0         UG    0      0        0 eth0

As you can see, my default gateway has an IP address of 192.168.0.1. In my case, that's my router, which talks to the LAN on one side and to the cable modem/Internet on the other.

At last

Last we have last. Enter it without any arguments and it will show you the last system signons to your system, going as far back as the log files allow it to go. The output looks like this:

warthawg pts/0 :0.0             Mon Jul 18 16:43   still logged in
warthawg pts/0 :0.0             Mon Jul 18 16:42 - 16:42  (00:00)
warthawg pts/0 :0.0             Mon Jul 18 16:40 - 16:40  (00:00)
warthawg pts/3 :0.0             Mon Jul 18 15:35 - 15:39  (00:04)
warthawg pts/2 :0.0             Mon Jul 18 14:47   still logged in
warthawg pts/0 :0.0             Mon Jul 18 14:36 - 16:34  (01:58)
warthawg pts/0 :0.0             Mon Jul 18 14:23 - 14:36  (00:13)
warthawg pts/0 :0.0             Mon Jul 18 14:20 - 14:23  (00:02)
warthawg pts/2 :0.0             Mon Jul 18 14:07 - 14:18  (00:11)
warthawg pts/2 :0.0             Mon Jul 18 13:44 - 13:45  (00:01)
warthawg pts/0 :0.0             Mon Jul 18 11:52 - 14:07  (02:15)

There you have it. A tease on seven different commands you can use to get information about your system. Many of them allow arguments that you can use to tune and modify the output. If any of these have caught your fancy, do the man thing.

Share    Print    Comments   

Comments

on CLI Magic: Information, please

Note: Comments are owned by the poster. We are not responsible for their content.

missing in action

Posted by: Anonymous Coward on July 25, 2005 05:39 PM
uname -a

#

missing commands that should have been mentioned

Posted by: Anonymous Coward on July 25, 2005 05:59 PM
These are simple commands that could have been mentioned:

which - shows the full path of the executable that would be executed. Useful to disambiguate if there are different executables in different paths with the same name.

uname - print system information. Use as
$ uname -a
to print all information

whoami - print currently used login. Useful when you are jumping via ssh from one system to another

hostname - print name of the host.

finger - report information about users on local and remote machines

file - report information about the type of a certain file.

lpq/lpstat - report status of print jobs

ps - show active processes in the system.

who - reports which users are logged in<nobr> <wbr></nobr>...

#

Re:missing commands that should have been mentione

Posted by: Anonymous Coward on July 26, 2005 12:51 AM
If you're looking for unix commands, try <a href="http://www.coolcommands.com/" title="coolcommands.com">http://www.coolcommands.com/</a coolcommands.com>

#

Not entirely useful

Posted by: Anonymous Coward on July 25, 2005 06:05 PM
whereis is alright,<nobr> <wbr></nobr>/if/ you're after all that information. What if you just want the first bit of information, the path of the executable? This is why we have which:
which whatis<nobr> <wbr></nobr>/usr/bin/whatis

And ofcourse df can be made more useful with the -h switch - it presents the sizes in figures that are a bit more human-readable: MBs/GBs where appropriate instead of KBs... I have a 30GB drive. This is quite small by today's standards. Why would I look at the used space on it in terms of kilobytes?

#

lower back pain

Posted by: Anonymous Coward on May 30, 2006 01:19 AM
[URL=http://nervepainrelief.jeeran.com/painrelief<nobr>.<wbr></nobr> htm] Nerve pain relief [/URL]

  [URL=http://www.back.painreliefnetwork.net/lowbac<nobr>k<wbr></nobr> pain.htm] Low back pain [/URL]

  [URL=http://blog.gala.net/uploads/painreliefback/<nobr>b<wbr></nobr> ackpainrelief.htm] Back pain relief [/URL]

  [URL=http://www.weblog.ro/usercontent/13155/profi<nobr>l<wbr></nobr> es/kneepainrelief.htm] Knee pain relief [/URL]

  [URL=http://www.info.painreliefnetwork.net/Pain-R<nobr>e<wbr></nobr> lief.html] Pain relief [/URL]

  [URL=http://www.sitefights.com/community/scifi/pa<nobr>i<wbr></nobr> nrelief/painreliefpreved.htm] Pain relief [/URL]

  [URL=http://www.info.painreliefnetwork.net/Medica<nobr>t<wbr></nobr> ion-Pain-Relief.html] Medication pain relief [/URL]

  [URL=http://www.info.painreliefnetwork.net/Natura<nobr>l<wbr></nobr> -Pain-Relief.html] Natural pain relief [/URL]


  [URL=http://painrelief.fanspace.com/index.htm] Pain relief [/URL]

  [URL=http://lowerbackpain.0pi.com/backpain.htm] Back Pain [/URL]

  [URL=http://painreliefproduct.guildspace.com] Pain relief [/URL]
[URL=http://painreliefmedic.friendpages.com] Pain relief [/URL]

#

kernelversion

Posted by: Anonymous Coward on July 25, 2005 06:15 PM
I don't have that command in my linux box.. maybe there are some exceptions.<nobr> <wbr></nobr>:D

#

Re:kernelversion

Posted by: Anonymous Coward on July 25, 2005 06:57 PM
Some distros have more or less commands included by default. Try uname -a instead. It even gives more info.

And, just a personal comment. I can't stand to find that a distro do not include grep or locate by default. It is really frustrating.

#

Re:kernelversion

Posted by: Anonymous Coward on July 25, 2005 08:43 PM
uname -r
lists also the kernel version

#

More useful stuff...

Posted by: Anonymous Coward on July 25, 2005 06:36 PM
...besides what already have been mentioned:
netstat (huge topic)
grep (huge topic)
cat
top
uptime

#

Do not forget Locate or (r)grep

Posted by: Anonymous Coward on July 25, 2005 06:53 PM
Not all distributions include locate by default. But it is really usefull.

Example: locate *HOWTO*

Finds all files that cointain the word HOWTO.

And grep & rgrep can be usefull too. Lets say that you are looking for al the files that opens a Mysql connection in your source code. Lets type

rgrep mysql_connect . ( assuming that al the source is in the directory . or subdirectories...)
I will look _inside_ the files. It's absolutely a time saver command.

#

Re:Do not forget Locate or (r)grep

Posted by: Administrator on July 26, 2005 04:34 AM
locate/slocate require you to build a database of information before you can use them. Most distro's have an "updatedb" command which must be run as root before locate will work.
NB -- If your distro includes/installs locate, it will most likely put updatedb into a cron job and run it for you, every night. This is fine on a desktop where files constantly come and go, but if you run a server with a ton of files, updatedb can bring your system to a screeching halt everynight for as long as it takes to run. In production environments, updatedb's cron job is best disabled, and updatedb run only when needed. At the very least, learn what the '-e' flag does to (GNU) updatedb.

#

Another one: apropos

Posted by: Anonymous Coward on July 25, 2005 07:02 PM
Example:

$ apropos alsa
aconnect (1) - ALSA sequencer connection manager
alsaconf (8) - configuration tool for the Advanced Linux Sound Architecture
alsactl (1) - advanced controls for ALSA soundcard driver
alsamixer (1) - soundcard mixer for ALSA soundcard driver, with ncurses interface
amidi (1) - read from and write to ALSA RawMIDI ports
amixer (1) - command-line mixer for ALSA soundcard driver
aplay (1) - command-line sound recorder and player for ALSA soundcard driver
arecord (1) - command-line sound recorder and player for ALSA soundcard driver
aseqnet (1) - ALSA sequencer connectors over network
speaker-test (1) - command-line speaker test tone generator for ALSA

#

Re:Another one: apropos

Posted by: Administrator on July 26, 2005 04:46 AM
Apropos, like the locate entry above, is a useful tool, but it requires you to build a database for it to work. Most distro's that use apropos use "makewhatis" to build the whatis db from existing man pages in the man path,usually in a cron job.


"whatis" (mentioned in the article itself) and "apropos" both work from the whatis database. "apropos" searches whatis for strings, "whatis" searches for complete words.


NB -- You can get the same functionality that apropos gives you by using "man -k"

#

Disk usage

Posted by: Anonymous Coward on July 25, 2005 08:03 PM
An excellent question to ask from time to time, and especially before a critical drive fills up completely. To get the answer, type df on the command line.

But what's taking up all that diskspace? 'du' provides the anwser:

[todsah@jib]~$ whatis du
du (1) - estimate file space usage
[todsah@jib]~$ du -hs *
93M Mail
32M Trash
426M bin
108M dev
357M download
1.8M notes
4.0K temp

And though it's not a CLI tool, XDU is still very handy. It shows a graphical representation of the output of the 'du' command:

<a href="http://sd.wareonearth.com/~phil/xdu/" title="wareonearth.com">http://sd.wareonearth.com/~phil/xdu/</a wareonearth.com>

#

Re:Disk usage

Posted by: Anonymous Coward on July 27, 2005 12:38 PM
If you like XDU, you'll love Filelight!
Requires KDE libraries.

<a href="http://www.methylblue.com/filelight/" title="methylblue.com">http://www.methylblue.com/filelight/</a methylblue.com>

#

A few of my favorites

Posted by: Anonymous Coward on July 27, 2005 01:26 AM
I like to use "which" inside of backticks. Some programs require the full path to a file in order to work; for example:

~$ file grep
grep: can't stat `grep' (No such file or directory).

But if I use which inside of backticks it works fine:

~$ file `which grep`<nobr> <wbr></nobr>/bin/grep: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), stripped

I also sometimes forget the name of some program but can remember part of it such as a lot of programs will have "conf" in them somewhere. If I want to see all of the programs that have "conf" in them I use:

~$ locate conf | grep bin/

And it gives me all of the programs that have "conf" in their names that are in the<nobr> <wbr></nobr>/bin<nobr> <wbr></nobr>/sbin<nobr> <wbr></nobr>/usr/bin<nobr> <wbr></nobr>/usr/sbin directories.

Cheers,

netcat

#

Re:A few of my favorites

Posted by: Joe Barr on July 27, 2005 11:22 PM
>But if I use which inside of backticks it works fine

Thanks, great tip!

#

Interesting site for unix commands

Posted by: Anonymous Coward on July 28, 2005 08:49 PM
If you're looking for unix commands, try <a href="http://www.coolcommands.com/" title="coolcommands.com">http://www.coolcommands.com/</a coolcommands.com>

#

Re:A few of my favorites

Posted by: Anonymous Coward on August 01, 2005 12:45 AM
Backticks are deprecated. Use $(command) instead of `command`.

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya