Linux Commands

 

General Cautions

 
Make sure you always know your 'working', or 'current', directory.  Use pwd (Print Working Directory) to see the full file path if there is any question about where your files need to go.  Students unaware of the function of mkdir & their 'working directory' might end up with a an excess of 'web directories' and a directory tree like /home/fname/web/web/web/web!
 
Don't start using vi until you've found a tutorial that you like on the web or in a text: 

If you're using vi make sure you always know which 'mode' you're in.  Use Esc to get out of Insert, Append, or Replace mode before trying to enter commands.  Make sure the bottom of your screen is visible so you can see what's going on at the bottom. Vi is a tool that makes tiny demands on an OS but expects the User to know what they're doing. There's probably the most _value_ in learning how to use it since it's part of every *ix distribution and routers and other network hardware is likely to use a similar 'line editor'. This is not Windoze, get over this hurdle...

Most Linux distros use 'vim', vi improved, instead of the plain, old vi when entered.  vim supports the PC's cursor movement and editing keys.  If you're experienced with vi from old *ix, check out vim's features -- it's no longer necessary to more the cursor using the h, j, k, & l keys!

Losing one's SSH connection while editing a file in vi causes files named with the 'dot' prefix and when next editing them you'll need to respond to vi's prompts about what to do with the situation.  VCUNet's border routers close an SSH connection after a few minutes' non-activity, so it's best to close up everything and not leave vi, or mc, hanging & needing recovery when you next log in.

 

Find and learn tech to use vi's u and ctrl-r to undo and redo. Use the V-visual mode to copy/paste.

There is no undo for most bash commands, if you say rm and answer y, it's gone forever.  Save often. To abandon the changes, q! will exit the file without writing it.

 

After you've logged into your home directory:
 
'man cmd', MANual, will give the manual page for 'cmd'.  Use it to find out more about any of these commands.  Space bar 'pages' the man display, 'b' makes it go back a page, 'q' gets out of the man display.

 
ps, Process Status, will show your processes, ps -ef will show yours and everybody else's.

 
pwd, Print Working Directory, will show which directory you're in. 

 
touch will create an empty file, or if it's not empty will update it's date/time stamp.  For example touch MyProjectTwo will create a file with that name.

 
Getting it to quit, exiting, paging, & other incidental keystrokes common to the commands that follow:

Unlike a GUI where there's always that X to click to get you out of something that's running, the *ix user has to know how to get out before they get into an application.  Where there is the 'three finger salute' in Windows to get to the 'task manager' so you can 'end' a task, Linux provides similar functions at the command line, where you can 'kill' a process.  Usually, the gentle *ix user will want to use the 'least force' necessary to stop a process, and here are some options from the gentlest 'q' thru 'kill'.

'q' is often used to Quit a utility or application that's prompting for input.  For example, 'top' will continue running until a lower-case q is entered.

'exit' is used to log out of the shell, and to exit some utilities and applications. 

'Ctrl-c' will stop most applications from running.  For example if you say 'ping 128.172.999.999' without specifying -n, you can use ctrl-c to stop the pinging, since q won't do it.  Ctrl-c is in someways like using a hammer to stop the stopwatch instead of using the device's stop button & some processes may not 'clean themselves up' properly if stopped with ctrl-c.

'Ctrl-d' means 'end of data' for a command like mail -s 'this is a test' somebody@somewhere.  Entering this command specifies the subject line and email address, then lets the User key in the body of the email, UNTIL they hit Ctrl-d to indicate they've finished the message.  Ctrl-d also gets you out of the situation where the command-line continuation character, the \ just above the enter key, is hit by mistake, changing the command line's prompt from # or $ to >, indicating that it's going to continue taking a multi-lined command until you hit ctrl-d to submit it.

'kill 12345' or 'kill -9 12345' allows su to kill any process, where the PID is often revealed by doing a ps -ef.

 

More consoles, scrolling backwards

When using Linux on the 'system console' (the PC's monitor), Alt + an FKey will open a new terminal session on the monitor.  Alt-F1 is the first, Alt-F2 the second, and so on.  This can be very helpful.  (It's possible to do this on a remote session, too, using 'screen', but it's just as easy to open another putty or other terminal session.

Shift-PageUp & Shift-PageDn let you scroll the screen back and forward.  This works on the Linux console, and also works with putty if you're working remotely.

 

Server-side text editors: vi, Visual Editor; mc, MidnightCommander; & nano:

Create a couple of text files so you can work with commands to move them around, look at their contents, and gain some expertise with server-side text editors.

vi is the 'classic' *is editor and it is distributed with all *ix you're likely to encounter.  It was developed for use on tty devices, 'dumb-terminals', virtual keyboards of iPhones and Droids, and every other character interface. These devices might not have cursor arrows, but do have a 'control key' like Ctrl and an escape key. Regular keys a-z are used for cursor-control, editor commands, and to insert, replace, or append text. 

vi MyProjectTwo will open the file in vi.  Insert text after touching i to get into Insert mod,; get back to command mode using Esc; Replace text after touching r or R to get into replace mode, Esc to get back to command mode; type : to get a little command line at the bottom of the screen, then get out of vi using :wq to write the file & quit, or q! to quit without writing the file. 

 

mc, Midnight Commander, is not classic *ix, nor is it distributed with all *ix, but it is a very handy navigator/editor/file-looker-atter worth mention for a Linux Admin.  It provides a user-friendly, Function Key & Control Key interface.  Use 'mc -a' if versions and/or settings of terminal windows result in an 'ugly' mc console for you.  The -a option forces mc to use ordinary characters like +, | and - to draw the console rather than the fancier-looking line drawing characters.  If some function keys, like F10 or Esc don't work, check your terminal window's settings.

Here is a tutorial for Midnight Commander, and googling will find others that are helpful.  F1 gets you to help pages that explain the basic functions.

The function of the Function Keys 1-10 is always displayed at the bottom of mc's screen.  Plus there is lots of other functionality available under the menus F9 pulls down, including the ability to write F3 selected stuff to a file, or insert the contents of a file, similar to copy/paste. 

The top line is always showing you exactly what line you're on.  Ctrl-o will blip you into and out of a 'plain command line' while you're editing a file or looking at a navigation pane.  Hitting tab moves you from one side of mc to the other, so you can show the contents of two directories and use F5 & 6 to copy and move files between them.  There's a lot more to say.  Find and read docs and HowTos about Midnight Commander.

Use the navigation features to get to the directory with your file, use the cursor arrows to highlight it & F4 (see the F keys at the bottom) to edit the file.  F9 gets the dropdown menus working. F10 to exit the document and again to exit mc. 

Navigate quickly using the PC keyboard's Home key to jump to the top of a directory so that Enter takes you to the prior directory.  Page Up and Down should work fine, too.

Note that mc provides extra stuff like the ascii value of the character under the cursor, and the exact position of any character in the file.

Let me know if you find an easier way to create a new file than typing 'touch new_file_name' at mc's little command line at the bottom. 

 

nano appears to be gaining favor, is distributed with Fedora and is mentioned here as a nice light-weight editor.

nano uses ctrl-key combinations for its commands so is somewhat easier to tackle than vi.

There is probably more value in becoming proficient with vi than either of the others. 

Real geeks will want to get into EMacs & other power-tools in the *ix legacy.  These tools were built for programmers by programmers and can make a very effective character-based 'IDE' for someone working with C++ or any of the other *ix programming languages.

 

touch SomeFileName will create an empty file with that name in the current directory.  If you're in mc you can use touch at mc's command line to create a file and it will show up in the directory list.

 
ls, LiSt, will list the contents of the current directory.  ls -lasi will list the contents and more info about the files including the permissions. 

 
more SomeFileName will show you the contents of the file, and if it's more than one page, will sho one page at a time.  Go to the next page by hitting the spacebar.  Go Back to a prior page by hitting b.  Quit by typing q. 

less also works to show a file, but doesn't have as many features as more.

cat SomeFileName will also display the file, and can be used with grep to 'filter' out what you want to see.

 
mkdir, MaKeDIRectory, will make a directory just below the current directory. 

For example, if you're in your /home directory, 'mkdir web' will make your web folder that is 'soft-linked' to your account at info491.net, making your the 'document root' for url info491.net/youruserid

 
cd, Change Directory, is used to get from one directory to another.  If you are in your home directory, cd web will get you to your web directory, and pwd will verify exactly where you are. 

To move 'back' one directory, perhaps from your home directory while you're in web, use 'cd ..'  The 'dot dot' means 'parent directory'. 

You can use a full file path, like cd /etc/xinetd.d, to get to any directory available to you.

cd ~, tilde, takes you to your directory in /home/

 

chmod +x web will apply a 1 for the owner, group, and world permissions. Applied to a directory, the 'x' allows owner, group, or world granted the x to put that directory on a path. Applied to a file, an 'x' makes its contents 'eXecutable' which makes it pop up as a command on the Users path ('set' will show this). Your web directory should be on a path valid for 'apache' when it serves your files, so /home/fname/web should have an x in the World column. The web directory's contents should be 'world readable', with an 'r' (4) in the World column.

 

mv, MoVe, is used to move files from one place to another, or to rename them.  mv MyFile ./web/MyFile would move MyFile from my home directory to the web directory.  The './' means 'this directory'. 

 
rm, ReMove, is used to delete files or directories. 'rm MyWrongFile' will ask if you want to delete the file and do it if you say yes. To delete a directory the -r option, for 'recursive', must be used, like 'rm -r MyWrongDirectory'.

 

Pipes & redirects: 

The 'pipe symbol', |, provides a way for one command to 'pipe' its output through another command, like grep or awk.  For example,

   cat /etc/services | grep 443

will show that https uses port 443

grep may be used with the -v option, inVerse, to show all lines that do not contain a phrase.  For example

   /usr/sbin/tcpdump | grep -v 128.172.188.203 will show all packets that do not have this address.

The > is used to redirect 'standard output' to a file instead of the console.  For example

   ps -ef > psnow

will put the process status report into a file named psnow.

Two >s (>>) will append output to a file.  So,

   ls -las >> psnow

will add the file listing after the process status in psnow.