Welcome

You have reached the blog of Keith Elder. Thank you for visiting! Feel free to click the twitter icon to the right and follow me on twitter.

Basic Unix Commands and Concepts

Posted by Keith Elder | Posted in Howtos, Linux, Open Source, Programming | Posted on 20-04-2001

Ahhhhh Linux

That sweet smell of plastic, gold, copper, and bunch of Hackers. It’s a sweet smell that I have come to love over the years. If you have never experienced the thrill and joy of a Linux command prompt, sit back and we’ll discover some of the basics of commanding a full blown Linux box all by ourselves. We will also touch on some basic and essential tools that are standard while using the Linux Operating System as well as touch on some concepts that will hopefully help the reader to better understand why things are the way they are.

Logging in

Unlike most operating systems many of our readers are familiar with, Linux was built from the ground up with the concept of having multiple users. In other words, multiple people can log into the same machine and issue different commands and have those commands processed by the system. This happens because of several reasons. One, is because of what is called multi-tasking. Linux is a true multi-tasking system which allows more than one thing to be going on at a time. I know this is a new concept for a lot of our readers but it is a fundamental principle as to how servers work. More on that later. As with most “unix” operating systems there is a user to which all others strive to be, he is called root. ROOT , if you will, is the dictator of his domain, the ruler and allmighty, the beginning and the end, the alpha and the omega (get the point?), root, is the commander and chief of his operating system.

Unlike Windows, Mac and Dos, Linux uses a system by which the “Dictator” or “root” assigns users of his domain a login. Logins are quite common place today and are used throughout the Internet to gain access to websites and other systems. Root assigns its users a “login name” and a “password” to which the user can gain access to the system. When a Linux system finishes booting (the process of completing its startup procedures), it will present the user with the following:

 Login Prompt: 

The user will then enter their assigned name, and their password (note: the password is not displayed when entered into the system). If the user has typed a valid user name and password, the user is presented with what we call a prompt. The prompt is where all information is entered by the user for the operating system to carry out, we call these commands. A prompt can be shaped in several different ways but typically ends in a “$“. Example:

 bash-2.04$ 

The prompt can of course be changed to display whatever you like because after all it is your prompt!.

Example of a cool prompt

 (zorka@zorka)::(mail) (~/mail):  

Ok, so I’ve got a cool prompt! Now show me what this thing can do. For starters, let’s start with the following commands. These commands will guide you through some very basic steps into controlling your Linux system.

A few basics

  • cd = Change Directory
    Example:
    root:~# cd /bin
    root:/bin#

    In our above example notice how our prompt changed to show us where we are currently at in the file structure. When using cd, the two following examples are completely different:

    cd /bin = with the “/” in front of bin this takes us back to the root directory
    cd bin = this basically takes us forward into the directory called bin

    Z-Trick
    – pressing the “tab” key when changing directories or typing files will autocomplete the directory or command for you, this is a built in feature of Linux (did we mention Linux people are lazy?)s = list

    [root@web /bin]# ls arch* dmesg* igawk* nice* stty* ash* dnsdomainname@ ipcalc* nisdomainname@ su* awk@ doexec* ipv6calc* open* sync* basename* domainname@ kill* ping* tar* bash* echo* linuxconf* ps* tcsh* bash2@ ed* ln* pwd* touch* bsh@ egrep* loadkeys* red@ true* cat* ex@ login* rm* umount* chgrp* false* ls* rmdir* uname* chmod* fgrep* mail* rpm* usleep* chown* gawk* mkdir* rvi@ vi@ consolechars* gawk-3.0.6* mknod* rview@ view@ cp* gettext* mktemp* sed* vim@ cpio* grep* more* setserial* vim-minimal* csh@ gtar@ mount* sfxload* ypdomainname@ date* gunzip* mv* sh@ zcat* dd* gzip* netconf@ sleep* zsh@ df* hostname* netstat* sort* [root@web /bin]# 

    In order to know what is “listed” in a directory or a folder as some operating systems call them we have to have a way to tell the operating system to tell us what is listed in its directory. Ls has many different options and parameters which we can use to either narrow what we would like to see listed, or give us what more than we can handle. Combining “ls” with other commands as we will see later will allow us to further narrow our listings. Here are a few of the most popular uses of how “ls” is used:

    ls -al = list all files in directory in long format

     [root@web /bin]# ls -al total 5944 drwxr-xr-x 2 root root 4096 Aug 6 08:56 ./ drwxr-xr-x 18 root root 4096 Nov 1 11:18 ../ -rwxr-xr-x 1 root root 2844 Apr 2 2001 arch* -rwxr-xr-x 1 root root 66556 Dec 5 2000 ash* lrwxrwxrwx 1 root root 4 Aug 6 08:38 awk -> gawk* -rwxr-xr-x 1 root root 6052 Feb 7 2001 basename* -rwxr-xr-x 1 root root 437052 Mar 29 2001 bash* lrwxrwxrwx 1 root root 4 Aug 6 08:37 bash2 -> bash* lrwxrwxrwx 1 root root 3 Aug 6 08:40 bsh -> ash* -rwxr-xr-x 1 root root 9752 Mar 31 2001 cat* -rwxr-xr-x 1 root root 12404 Mar 30 2001 chgrp* -rwxr-xr-x 1 root root 14756 Mar 30 2001 chmod* -rwxr-xr-x 1 root root 11984 Mar 30 2001 chown* -rwxr-xr-x 1 root root 41308 Apr 11 2001 consolechars* 

    ls -Ral = same as above but list contents of all subdirectories

    Z-Trick – By using the built in online help of Linux, we can find out more about the “ls” command and many more by typing “man ls”. man stands for Manual and will search the man pages for the “ls” man page and display its results. If ever in doubt about what a command does try “man
  • nt path
     bash-2.05$ pwd /usr/local/bin bash-2.05$ 
  • passwd = change current password
     bash-2.05$ passwd
    Changing password for zorka
    (current) UNIX password:
  • mkdir = make a directory
  • cd .. = go back to the previous directory
    Without getting into a lengthy explanation of file pointers and those types of things, if you look at an above ls example you will notice there are two files listed when doing an ls -al, they are ” . ” and ” .. “. Dot and Dot Dot as it refered to refer to the previous directory and the current directory.
  • rm = used in combination to remove files and directories
    The “rm” command does a lot and is very powerful. NOTE: There is NO undelete command in Linux so be CAREFUL!. A couple of examples:

    rm -f = force the removal of a file (in other don’t ask me “Are you sure?”)
    rm -Rf = remove recursively and do not ask me

  • man = review the man page for a certain command
  • tenate (out file or redirect to standard output)
  • more = used to pagenate output on a terminal
    cat file.txt | more
  • ln = symbolic link
    Without going into a lot of detail about inodes and symlinks, think of creating a ln (or link as it is refered to) as creating a “Shortcut” in Windows. A link simply points to or links to the original file.
  • mv = move a file
    mv file.txt /tmp/file2.txt
  • > = redirect output
    ex: ls -al > dump.txt
    This will list the files in a directory and store the listing into a file called dump.txt.
  • < indirect
    Used to read in a file into a program
    mysql -u user -p password < dump.sql
  • >>= append to current file
    ex: cat *.txt >> alltextfiles.txt
    This will read all of the .txt files in a directory and combine them into one file called alltextfiles.txt.
  • du = shows total disk usage for current path
  • df = show amount of free hard drive space
  • ps = view prcoess
    ps aux | grep apache
    This will find the process where apache is running and display it pid among other useful information.

     [root@web /bin]# ps aux | grep apache apache 2048 0.0 1.4 6696 2784 ? S Jan03 0:00 httpd-perl -f /et apache 2049 0.0 1.4 6696 2784 ? S Jan03 0:00 httpd-perl -f /et apache 2050 0.0 1.4 6696 2784 ? S Jan03 0:00 httpd-perl -f /et apache 2051 0.0 1.4 6696 2784 ? S Jan03 0:00 httpd-perl -f /et apache 2064 0.0 2.6 40880 4976 ? S Jan03 0:48 httpd -DPERLPROXI apache 2065 0.0 2.6 40964 4988 ? S Jan03 0:45 httpd -DPERLPROXI apache 2066 0.0 2.6 40908 5004 ? S Jan03 0:43 httpd -DPERLPROXI apache 2067 0.0 2.5 40896 4968 ? S Jan03 0:56 httpd -DPERLPROXI apache 2070 0.0 2.5 40872 4920 ? S Jan03 0:47 httpd -DPERLPROXI apache 2087 0.0 2.6 40960 4980 ? S Jan03 0:56 httpd -DPERLPROXI apache 2236 0.0 2.6 40884 4980 ? S Jan03 0:46 httpd -DPERLPROXI apache 13810 0.0 2.5 40856 4888 ? S Jan04 0:33 httpd -DPERLPROXI apache 24923 0.0 2.6 41080 5124 ? S Jan05 0:26 httpd -DPERLPROXI apache 25083 0.0 2.5 40892 4928 ? S Jan05 0:18 httpd -DPERLPROXI root 21725 0.0 0.3 1636 620 pts/0 S 19:17 0:00 grep apache [root@web /bin]# 
  • kill = kill a certain process
    In the above command we found out how to find a process of a certain command. Now we can combine that with the kill command. kill -9 5267
  • top = view running processes
    Used to see which processes are using the most resources

     7:03pm up 4 days, 9:01, 3 users, load average: 0.01, 0.02, 0.00 99 processes: 98 sleeping, 1 running, 0 zombie, 0 stopped CPU states: 4.4% user, 10.9% system, 0.0% nice, 5.0% idle Mem: 191312K av, 189476K used, 1836K free, 0K shrd, 2916K buff Swap: 257032K av, 2328K used, 254704K free 51948K cached PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME COMMAND 21714 root 19 0 1096 1096 844 R 8.1 0.5 0:00 top 1 root 8 0 532 532 464 S 0.0 0.2 0:04 init 2 root 9 0 0 0 0 SW 0.0 0.0 0:00 keventd 3 root 9 0 0 0 0 SW 0.0 0.0 650:21 kapm-idled 4 root 9 0 0 0 0 SW 0.0 0.0 0:03 kswapd 5 root 9 0 0 0 0 SW 0.0 0.0 0:00 kreclaimd 6 root 9 0 0 0 0 SW 0.0 0.0 0:00 bdflush 7 root 9 0 0 0 0 SW 0.0 0.0 0:00 kupdated 8 root -1 -20 0 0 0 SW< 0.0 0.0 0:00 mdrecoveryd 63 root 9 0 364 360 308 S 0.0 0.1 0:00 open 68 root 9 0 1876 1876 528 S 0.0 0.9 0:07 Monitor-NewStyl 691 root 9 0 0 0 0 SW 0.0 0.0 0:00 eth0 747 rpc 9 0 508 508 424 S 0.0 0.2 0:04 portmap 759 root 9 0 564 564 456 S 0.0 0.2 0:35 syslogd 768 root 9 0 1088 1088 452 S 0.0 0.5 0:00 klogd 783 rpcuser 9 0 752 752 648 S 0.0 0.3 0:00 rpc.statd 879 root 9 0 980 948 764 S 0.0 0.4 0:00 xinetd 
  • tail or tail -f
    Outputs the last 10-15 lines of a file
    tail -f /var/log/access_log
  • tar (tar xvfz or tar cvfz) = uncompress, decompress a folder and its contents
    tar xvfz netscape.tar.gz = uncompresses the file
    tar cvfzpP netscape.tar.gz /usr/local/netscape = create a compressed tar file
  • telnet = phone your computer
    telnet work.somewhere.com
    NOTE: Not all machines have remote telnet access enabled.
  • ssh = a secure tunnelling protocol used to make a secure connection from one PC to another, currently replacing telnet
    ssh -l username remote.host
    ssh -l george home.george.com
  • mount = used to mount your media devices so they are veiwable from the os (also umount)
  • crontab = used to schedule jobs at certain times and intervals
  • chown = change ownership of a file
    chown root.user /tmp/
  • chmod = change the permissions of a file
    chmod 755 file.html
    chmod 644 file.html

If you would like to spend more hours of finding ways to waste your evening, a great place to start is the /sbin, /usr/sbin/, /usr/bin/, /bin/ directories. These directories hold all of the executable files that you can explore. Normally by typing man Welsh

 

Using the vi editor to edit your files

This document is meant to serve as a quick reference and as an introduction to the vi editor. A much more detailed description that covers all of the features of vi can be found online here at Purdue University.

The vi editor is one of the most powerful editors available in the unix platform. Is it the easist to learn? Probably not. But a few quick tips can put us on track to editing our documents with ease. Before long you will be working on multiple documents, copying and pasting text and using other advanced features. Let’s begin.

The Game
Vi is a fun program to master as it involves a fundamental technique of typing. One of the first things all of us learned when we took are first typing lesson was about “home row”. Home row for those of you that may have skipped typing is the process by which the typist places their left hand on “a s d f” keys and the right hand on the ” j k l ; ” keys while the thumbs rest on the space bar. The key to speed in typing is to never look at the keys and never take your hands off of home row. Again a basic fundamental technique. This also aplies to using vi. Vi was built so that you can navigate a document and never take your hands from home row. This doesn’t go to say this is always true as some higher level uses of vi may require you to press 3 keys at the same time. For general purposes though, 99% of your time will be spent on home row while using vi, this is one of the reasons vi make files so extremely fast to edit. So, remember, if you take your hands off of home row you’ve lost the game and try to catch yourself hitting the arrow keys, you’ll be surprised.

Modes
Vi uses what it calls modes to distinguish how it will react to what the user wants. One of the first things you will notice with vi is that from the beginning if you open vi, you cannot actually start typing! In order to start typing we have to be in a “mode” that allows us to type. The mode for inserting text into our document is called “insert mode”. We can put our editor into this mode by simply pressing ” i “. After we are finished entering text, we have to press “esc” or escape in order to get out of insert mode. There are other command modes as well, like ” d ” for delete. Let’s just start with the basics.

Modes
i,a = insert text before/after cursor
d = delete mode
c = change mode

Moving around the file
h,j,k,l = left, down, up right, left
w,W,b,B = forward, backward by word
e,E= end of word
ENTER = returns cursor to next oline if in insert mode
0,$ = jumps the cursor to the first and last character of a line
ctrl-f, ctrl-b = scroll forward and back one screen
ctrl-d, ctrl-u = scroll down and up a half screen
ctrl-l = redraw screen
G = end of document
:(n) = replace (n) with a number and press enter, vi will jump to that line number

Other Tricks
J = justify, if your text is on different lines and all needs to be justified up onto the next line use this command
o,O = open new line for text below, above cursor
r = replace character
x = delete character under cursor
p,P = put delted text after, before cursor
y = yank (copy) word
yy = yank current line
p,P = put yanked text after, before cursor
u,U = undo last edit, restore current line

Combining Modes and Movement to get a little more fancy
We know that ” d ” puts us in delete mode, and we learned above that ” w ” will move forward a word. With vi we can combine this knowledge to work in our favor. Here are few examples of how we can combine modes and movements to do some more advanced editing of our documents.

dw = delete a word
dd = delete current line
dG = delete everything from cursor to end of the file
d(n)d = delete the next (n) lines
d(n)w = delete the next (n) words
cw = change word
cc = change current line

Saving, quiting, opening, and reading in a file
:w = write file
:w! = write your file if :w doesn’t work, the ” ! ” means to overide
:q = quite file
:q! = if changes have been made to a file vi will not allow you to exit, use the overide command if you wish to exit and not save changes
:r = read a file in, handy if you need to copy parts of one file into your existing document
:e = open a new or pre-existing file (your current document does not disappear, use the next command to go back to your already opened document
ctrl-shift-6 = switch between open files

Searching and Replacing
/ = search
:%s///g = search and replace
Example: :%s/foo/foo2/g = replace all instances of foo with foo2 in document

We have covered a lot of material today, only practice makes perfect! If you would like to pick up a book that covers all of vi’s commands I highly suggest the following two options:

Learning the VI Editor, Edition 6
Vi Pocket Reference

Comments (1)

dd

Write a comment