Simple Unix/Linux commands tutorial
Results 1 to 3 of 3

Thread: Simple Unix/Linux commands tutorial

  1. #1
    Senior Member
    Join Date
    Nov 2001
    Posts
    185

    Simple Unix/Linux commands tutorial

    Simple Unix navigation for those new to Unix like systems.

    Unix systems have a large amount of documentation that is installed with the base system. I would encourage you to read these manuals frequently and become an expert with the commands that you use in your daily work. Knowing a handfull of commands and all of their options can make you a virtual power user. There is so much to know about each of these commands, and to list them in their entirty would be rediculous here on AO. So I will show you some basic navigation commands and how to read the man pages for yourself for more detail.

    1. The man command.
    On the command line we can display the manual for any command that we want to know more about by simply entering `man whatever-you-want-to-know'.
    example usage:

    $ man ls

    # this would display the manual for the ls command.

    I would like to just mention the man -k option for the man command. It will come in handy in your journey through this new OS.
    example usage:

    $ man -k print

    example output: (trimmed down for some degree of brevity in this tut)

    grep, egrep, fgrep (1) - print lines matching a pattern
    grolbp (1) - groff driver for Canon CAPSL printers (LBP-4 and LBP-8 series laser printers).
    hostid (1) - print the numeric identifier for the current host
    hpdj (1) - Ghostscript device driver for printers understanding PCL 3+
    hpwd (1) - print the full path to the current HFS working directory
    infocmp (1m) - compare or print out terminfo descriptions
    innstat (8) - print snapshot of INN system
    isalnum, isalpha, isascii, isblank, iscntrl, isdigit, isgraph, islower, isprint, ispunct, isspace, isupper, isxdigit (3) - character classification routines
    iswprint (3) - test for printing wide character
    ldd (1) - print shared library dependencies
    logname (1) - print user's login name
    lp (4) - line printer devices
    lpc (8) - line printer control program
    lpd (8) - line printer spooler daemon
    lpr (1) - off line print
    lprm (1) - remove jobs from the line printer spooling queue
    lptest (1) - generate lineprinter ripple pattern
    mailq (1) - print the mail queue
    mcprint (3x) - ship binary data to printer
    menu_requestname (3x) - handle printable menu request names

    The output of `man-k print' is this example gives us a listing of all commands that have the word print in their title. This useful if for example you did not know the command to remove jobs from the print queue, you could skimm through the output and notice that `lprm' does that function. You could then do a `man lprm' to learn how to use the lprm command. Here we see that the output of `man-k' can be long if we just give it a single word to search for. So... that brings me to our first command that all new users shoulld use liberally.

    2.`more' and `less'

    more Dispalys the contetnts of whatever was entered at the command line one screenfull at a time. Pressing the <ENTER> key will scroll one line down at a time, and pressing the <SPACE> key will scroll down one screenfull at a time. The [b] key skips backwards one screen at a time, and the <q> key quits and returns you to a command prompt. See `man more' for a more complete list of options to use with the more command.
    example usage:
    $ man -k print | more

    #displays the output of `man -k print' one screen at a time.


    less Displays the contents of whatever was entered at the command line one screenfull at a time in the same manner as the more command, and uses the same simple navigation keys to move through the screen and quit. The less command differs from more in advanced functionality. See man less for a more complete list of options to use with the less command.
    example usage:
    $ man -k print | less

    #displays the output of `man -k print` one screen at a time.


    #note : the | is called a pipe, you can make that symbol by pressing the <SHIFT + backslash> key combo. On most displays it looks like a single vertical line, but on your keyboard it will probably look like two vertical dashes one over the other.

    3. The `ls' command

    The ls command is similar to the `dir' command that you used in DOS.
    example usage:

    $ ls

    #displays the contents of your current directory.

    $ ls -l

    #displays the contents of your directory in long format.

    $ ls -a

    #displays the contents of your directory including hidden files.

    $ ls -F

    #displays the contents of your directory, placing an indicator behind the name to tell you what the object is. An * is an an executable, a / is a directory, and no indicator is a text file.

    $ ls -t

    #display the conents of your directory in order from newest to oldest.

    $ ls -alFt

    #would display the contents of our current directory with all of the afore mentioned option in effect.

    See `man ls' for a more detailed description of the ls command.

    4. The `cd' command.

    This one that should be familiar to any of you who have used DOS much. The `cd' command simply changes directory to whatever is specified after it. In Unix like systems if we issue the `cd' command all by itself with nothing after it, it changes us to our home directory.

    Another thing that is handy to know is about . and .. , . represents our current directory, and .. represents our parent directory. Don't worry if you are confused by this, I will show you an example soon.

    Lastly before some examples, the ~ symbol represents our home directory. So.. if I am logged into the system as UberC0der, and my home directory is /home/UberC0der, placing a ~ in front of the rest of the path is the same as typing /home/UberC0der. Here again, i will show an example.
    example usage:

    $ cd

    # changes me to my home directory, in this case /home/UberC0der.

    $ cd ..

    #would change my directory to /home, this is because /home is the parent directory of /home/UberC0der.

    Now for example sake, let's say I am in /usr/local/games and I want to go to /home/UberC0der/games with the shortest number of keystrokes. I am lazy, so I am always looking for a shortcut.

    $ cd ~/games

    #because ~ represents /home/UberC0der all I have to do is type ~/games which would be the same thing as /home/UberC0der/games.

    ., .., and ~ are part of what is known as a relative path in the Unix world. The reason I mention them is that they are very handy and quick when working on the command line. Once again, I am a very lazy boy and I work mostly on the command line.

    Let's say my current directory is /usr/local/games/quake3 and I want to change to /usr/local/games. I could do:

    $ cd /usr/local/games

    -- or I could take a `relative path' to /usr/local/games --

    $ cd ..

    # I was in /usr/local/games/quake3 and now I am in /usr/local/games.


    Let's make it more interesting. I am in /usr/local/games/quake3/baseq3, and I want to go to /usr/local/games/quake2/baseq2 in the shortest number of keystrokes.

    $ cd /usr/local/games/quake2/baseq2

    # the long way

    -- or --

    $ cd ../../quake2/baseq2

    # taking a relative path


    5. The `mkdir' command

    The mkdir command creates a directory wherever you want to put it. Part of being a mutliuser system and the basic Unix security model is that you can only do this in parent directories where you have write premissions. I will cover Unix files permissions in another tut, but for now we are going to use our own home directory for the example.

    Ok, I am in my home directory /home/UberC0der. I want to create a directory called AnitOnline where I will store all of my favorite tut's that have been contributed by members. So using the mkdir command...
    0
    $ mkdir AnitOnline

    Now I want to make a directory called Tutorials under AntiOnline.

    $ mkdir AntiOnline/Tutorials

    Now I want to make a directory under AntiOnline called Posts.

    $ mkdir AntiOnline/Posts

    For example and a bit of humor lets make a directory under Posts called Flames where I will store all of my favorite poetry by Negative.

    $ mkdir AntiOnline/Posts/Flames

    #note 1: I only have to put a / at the beginning if I am specifying the path from the root ( / ) directory. If no path is specified it is assumed that I want to make this directory under my current directory. So starting from / the full path of AntiOnline would be /home/UberC0der/AntiOnline

    #note 2: I name Directories that I made on the system with a Captial letter at the beginnig of the directory name. The standard Unix converntion is that everything is lower case. This just helps me recognize a directory that I created if I were ever in doubt.


    6. The `rm' command

    *****WARNING***** Deleting files in Unix systems is PERMANENT. There is no `Recycle Bin' or any such feature. If you delete a file form a Unix system it is GONE. There are ways to recover delted files from a Unix file system, but for newbies you may as well consider the act a permanent thing and get used to the idea of deciding for sure whether you really want to delete that file.

    Okay, with that out of the way lets talk about the `rm' command. Once again, if you were a DOS user `rm' is much like `DEL' with some exceptions. It is a very basic deleting utility that has some very powerful options. For more details see `man rm'.

    As with many other utilities in Unix systems, the built in security will not allow you to use this command on any files that are not owned by you.

    To remove a file we would do:

    $ rm filename

    To remove a directory and all of it's contents we would do:

    $ rm -r directory-name

    #the -r option means recursive.

    I recommend that you use this command sparingly.

    7. The `cp' command

    The `cp' command is the copy command in Unix systems. It has some pretty nifty functionality. For more information see `man cp'.

    To copy a file and name it something else we would do:

    $ cp file1 file2

    # this makes an exact copy of one file and names it another name.

    To copy the contents of a directory to another location we would do:

    # In this example I have made a directory called /home/UberC0der/Good-Posts for example, I already have some posts in that directory and I want to copy the contents of /home/UberC0der/AntiOnline/Posts to this directory.

    $ cp -r AntiOnline/Posts Good-Posts

    #the -r option means recursive


    8. The `mv' command

    The `mv' command can do one of two things, it can move a file to a new location, or it can rename a file to another name. See `man mv' for more details on using the mv command.
    example usage:

    $ mv AnitOnline/Posts/new-post.txt AntiOnline/Posts/good-post.txt

    # renames the file new-post.txt under the directory AntiOnline/Posts to it's new name good-past.txt in the same directory.

    $ mv AntiOnline/Posts/good-post.txt ~/Good-Posts/good-post.txt

    # moves the file good-post.txt under the AnitOnline/Posts directory to it's new location ~/Good-Posts. Remember that I use the ~ to mean the same thing as /home/UberC0der.




    This is a lot to take in and play around with, so I will end this post here and will continue it with some other common Unix commands in a future tut, in addition to a tut on Unix file permissions.

    Merry Christmas everyone
    Know this..., you may not by thyself in pride claim the Mantle of Wizardry; that way lies only Bogosity without End.

    Rather must you Become, and Become, and Become, until Hackers respect thy Power, and other Wizards hail thee as a Brother or Sister in Wisdom, and you wake up and realize that the Mantle hath lain unknown upon thy Shoulders since you knew not when.


  2. #2
    PHP/PostgreSQL guy
    Join Date
    Dec 2001
    Posts
    1,164
    UberC0der! Not the ls command! Hehe, as my boss said once, "Senior administrators are defined by knowing 10 more flags on the ls command than regular administrators"...

    Very good tutorial...it gets someone off the ground and is duly worth replying to.
    We the willing, led by the unknowing, have been doing the impossible for the ungrateful. We have done so much with so little for so long that we are now qualified to do just about anything with almost nothing.

  3. #3
    PHP/PostgreSQL guy
    Join Date
    Dec 2001
    Posts
    1,164
    Oh yeah, one thing in *case* this is the problem.

    Problem:
    $ man blah-blah-blah
    man: command not found

    Solution:
    The 'man' command is usually in /usr/bin, so 'echo $PATH' should show you that /usr/bin is in there.

    $ echo $PATH
    PATH_GOES_HERE:/usr/bin

    If that's the case, you have no man! (sorry to hear that, hyuck hyuck). It would have to be installed.

    If you don't have /usr/bin in your path (as stated generally in /etc/PATH), then this will temporarily fix it (until you log out):

    $ PATH=$PATH:/usr/bin && export PATH

    This will put /usr/bin at the end of your path and now you should have 'man' usages. The 'export' command allows any shell you invoke or anything that reads in your variables defined in 'env' to use them.

    Problem:
    If you get things like 'No manual entry for (very common command like ls, grep, awk, etc), check the following:

    $ echo $MANPATH
    (should be a long string here)

    If not, you need to check /etc/MANPATH (HP/RH have these) or wherever your manpage variable is set.

    Just thought I'd add these on, cuz I know I was stumped when I couldn't do 'man man' hehe....
    We the willing, led by the unknowing, have been doing the impossible for the ungrateful. We have done so much with so little for so long that we are now qualified to do just about anything with almost nothing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides