Introduction to Unix (Linux and Mac OSX™)

[Sep 04, 2008 18:43] Emergency downtime - Central Exchange Service mailstore 3 is experiencing a problem and must be taken offline. Time-to-resolution is not yet known. ITC is working to resolve the problem.

[Sep 04, 2008 17:32] Access to ITC ESERVICES Exchange Mailstore 7 will be intermittent for the next 2-4 hours as we check to make sure the mailstore remounted correctly.

Modern Linux (based on Unix™) has a graphical desktop. It even has some themes and skins that make it look and work identical to Windows™ or Macintosh™. Mac OSX™ is a Unix-variant, is therefore Linux in most regards.

In spite of the graphical desktops, there are still many powerful features only available at the command line. There are all kinds of wonderful and useful tools.

We will start with an introduction to command line and keyboard operations and file system navigation. Then we'll move on to some useful tricks.

Introduction

In this document, the first time a new command or term is used, it will appear surrounded by double quotes. For example, the command line is also known as the shell. We will assume you are using the "bash" shell, which is the most modern "shell", and is the default in Linux and OSX.

The first thing you see is a "prompt" which is a string of characters used by the computer to show that is it ready for a command. The computer is waiting for you to type a command. All commands have at least one word, and you must press the enter key to launch the command. There is no harm in pressing the enter key without a command (in fact many experienced users hit the enter key a few times just to assure themselves that the computer is listening).

Different parts of the command are separated by spaces. Extra spaces are usually ok.

The "control key" is important in bash. Use it like the shift key. To enter control-c, hold down control and press c. Sometimes control-c is written ctrl-c or ^c or even occasionally (and incorrectly) in upper case Control-C. Bash is almost universally lowercase, and it is case sensitive. Do not use upper case characters unless there is a reason (such as an uppercase character in a file name, or a command switch is uppercase). Bash has all kinds of handy control characters that it recognizes, but ^c is the most important.

If you type a command, but change your mind ^c will cancel. If a command is running and hasn't completed (or is going wild) ^c will stop the command. It is ok to type ^c several times. Bash is stopped with the "exit" command or ^d (control-d).

The most common commands fall into broad categories. Your graphical desktop has functions to do the same things as the most common bash commands. The really interesting stuff you can do at the command line is not possible with most graphical desktops.

We will also use the command syntax description that you'll find in "man pages". Anything in [] is optional. "Switches" are preceded by one hyphen, and switches with long names have double hyphens. Switches enable or disable extra features of the command. You must type the hyphen/double hyphens as necessary. You must not have a space between the hyphen and the text of the switch. Some of the syntax is not literal text, but refers to a variable. Anything variable (like a file name) will be explained in the help for that command. It is sometimes tricky to determine what is literal text and not. Examples should clarify this.

For historical reasons (and consistency with other documentation) we refer to "folders" as "directories".

Navigating

This is done with one command "cd" (change directory). The format is:

cd [-L|-P] [dir]

Change directory to dir. There are some special directory names:

  • The directory immediately up (parent directory) is ..
  • The current directory is (dot) .

You can change several directories down or up by using a longer "path".

cd public_html
Changes to the public_html directory.

cd public_html/images
Changes to the images directory in public_html.

cd ../../.ssh
Changes up two directories into a directory .ssh (yes, the name begins with a dot).

Listing directory contents

ls [OPTION]... [FILE]...

ls (list sorted) has many options, and can take many files as arguments.

ls
A plain listing of the current directory.

ls -l
Long listing of the current directory

ls -la
Long listing, and include "hidden" files with dot at the beginning of their name.

ls -l *
List everything and also list contents of directories in this directory.

ls public_html/images
List the files in the images directory in the public_html directory.

ls -l public_html/images/*.jpg
Only list .jpg files in the images directory.

ls -alt
List all the files, starting with newest.

For more exciting reading see man ls.

Copying files

  • cp [OPTION]... SOURCE DEST
  • cp [OPTION]... SOURCE... DIRECTORY
  • cp [OPTION]... --target-directory=DIRECTORY SOURCE...

Copy a file from source to destination. Works also to copy a file into a directory. cp is smart enough to know not to overwrite a directory with a file (that's impossible anyway).

cp index.html index.html.safe
Copy our default home page index.html to index.html.safe. It is ok to have a file with two extensions, however, the web server won't know that a .safe is really a .html file.

cp index.html ../
Copy index.html up a directory.

cp has funny behavior when copying directories. If you want the directory and all the subdirectories copies, you have to use the -r switch:

cp -r public_html backup_public_html

Renaming files

  • mv [OPTION]... SOURCE DEST
  • mv [OPTION]... SOURCE... DIRECTORY
  • mv [OPTION]... --target-directory=DIRECTORY SOURCE...

mv works like cp. However, mv will rename directories.

mv query.pl query.cgi
Rename (move) query.pl to query.cgi

mv query.pl /home/mst3k/query.pl
Move query.pl to the home directory of mst3k.

Unlike what you might expect, you can't use mv to change the extensions of files.

mv *.pl *.cgi
This won't work. In fact mv will give a message:

"mv: when moving multiple files, last argument must be a directory"

There is a "rename" utility for wild card renaming. Be careful with rename since is can make a real mess. In fact, it is off topic, but pertinent: before trying any command that may destroy files, it is good to make a backup. The "tar" utility is good for this.

tar -cvf tmp.tar *.pl
Will create the tar archive tmp.tar from all the .pl files in this directory.

rename looks like this:

rename [from] [to] [file]...

"from" is a part of a file name, "to" is what you want the matching part changed to, and "file..." is a file or list of files.

To use rename for the mv example above that doesn't work:

rename .pl .cgi *.pl

Viewing files

This is where the fun starts. Text files are viewed with "more" or "less". Special files like images and word processing are viewed with the usual image editors and word processors, so we won't cover those.

It turns out that "less" has more features than "more" which leads to the geeky joke "less is more".

  • more [-dlfpcsu] [-num] [+/ pattern] [+ linenum] [file ...]
  • less -?
  • less --help
  • less -V
  • less --version
  • less [-[+]aBcCdeEfFgGiIJLmMnNqQrRsSuUVwWX~] [-b space] [-h lines] [-j line] [-k keyfile] [-{oO} logfile] [-p pattern] [-P prompt] [-t tag] [-T tagsfile] [-x tab,...] [-y lines] [-[z] lines] [-# shift] [+[+]cmd] [--] [filename]...

That's a lot of information. The usual case is just

more query.pl
less query.pl

Both will scroll the file onto the screen, stopping after each screen. Press "space bar" for the next screen. Press "enter" to move down one line. Press "q" to exit.

There is one difference between less and more: After it exits, the text more displayed stays on the screen.

less has all kinds of additional abilities. You can use the cursor keys and page-up/page-down with less. less can also search forward and backward. To search forward, press / (forward slash, below the ?), then enter some text and press enter. Press "n" to find that same text again, and "b" to find previous. less is case sensitive. Enter "-i" and press enter to switch less to case insensitive mode. If less dosn't seem to be in command mode use ^c or ":" (colon). less will not be killed by ^c, so you'll have to exit with q.

more only works on text files. less will display binary files, and in my experience won't mess up your terminal settings (as might happen with "cat" on a binary file).

You can also display files with the text editors vi (vim) and emacs. Both of those utilities are complex enough to deserve their own documentation. In modern version of both you can probably scroll around with the arrow keys and page-up/page-down. Exit from emacs with ^x^c and exit from vi by entering command mode with Esc "escape" or ":" and quit with q.

Deleting files is dangerous in Linux and BSD because it's creators have not seen fit to allow undelete or to implement a trash can. This is just one of many cases of snobbery amongst Linux and BSD people. They have all kinds of justifications, and they clearly aren't about to change, so we'll have to work around the problem.

Perhaps the safest thing is to simply mv files to your favorite trash folder.

mv query.pl ~/Desktop/Trash
move query.pl to Desktop/Trash in my home directory. ~/ is short hand for "my home directory".

When you want to get rid of a file, use rm.

rm query.pl
Remove query.pl

Often the "rm" command has been aliased to "rm -i" which is "interactive" which means it asks before doing anything. Answer with y and enter to delete the file or n and enter (or just enter) to keep the file.

[mst3k@tull ~]$ rm -i query.pl
rm: remove regular file `query.pl'? y
[mst3k@tull ~]$

The most dangerous command is "rm -fr *" which is rm force, recurse all files. "force" is don't ask, "recurse" is go into all subdirectories. This command can delete every file you own. If you are logged in as root, you can delete every file on the disk.

If you want to use rm -fr, follow these steps for safety (and maybe make a tar file first). We'll clean up everything in the "mars" directory. The commands below will: cd into mars, check that we really are in the mars directory (pwd is "print working directory"), ls the directory to make sure it really has the files we think it has, then use a variation of rm -fr that deletes files starting in this directory, and at last we ls and that everything is gone. ls -l might have been a better command. You can't tell here, but "sol62_fr" and "front_right" are both directories with files in them. All those files were deleted.

[mst3k@tull ~]$ cd mars
[mst3k@tull mars]$
[mst3k@tull mars]$ pwd
/home/mst3k/mars
[mst3k@tull mars]$ ls
2R131787621EFF1200P1310R0M1-BR.JPG 2R131962287EFF1400P1310R0M1-BR.JPG sol62_fr
2R131871671EFF1300P1310R0M1-BR.JPG front_right
[mst3k@tull mars]$ rm -fr ./*
[mst3k@tull mars]$ ls
[mst3k@tull mars]$

This kind of problem can lead to needing more interesting commands. If you want to ls -l a very large number of files, the list will scroll off the screen. Linux and BSD commands can be "piped" together. The output of one command is the input of another. In general this falls into the categories of "piping output" and "i/o redirection" (some spelled "io") for input/output. Pipes use the pipe character "|" which is above the "\" (backslash).

ls -l | less
A long list all files piped to less. Now you can scroll up and down the list with less, and even search. You could send the list to a file with i/o redirection.

ls -l > ls_out.txt
less ls_out.txt
Make a long listing, send it to the file ls_out.txt, then less the file. Capturing output is very handy when it will take a long time to run a command. For instance, getting a list of packages that yum can install:

yum list available > yum_list.txt
The > will take output and redirect it to a file. The file is overwritten, which means that the contents of the file are destroyed. There is no undo. You often want to overwrite temporary files. However, if you have something like a log file, you probably want to append output (concatenate). Use the >> redirection.

ls -alt >> big_list.txt

Make a date sorted long list of all files, and append it to the end of big_list.txt.

Searching inside files

The utility grep (get regular expression and print) will search for regular expression matches inside files. grep has many uses, and we'll only cover a few more common examples.

Regular expressions are a way of describing simple or complex wild card text matches. However, the regex * is not a wild card. Instead "." (dot) matches any single character. Perhaps the best way to use grep (at least on Linux) is to rely on simple regular expressions and if you need something complex, use the Perl regex switch and read Perl's perlre man page. I don't think BSD is using the GNU grep, so they probably don't have Perl regex's available.

grep tcp /etc/services
Will search /etc/services for "tcp", and print every line that matches.

ls /etc/ | grep serv
Will list all the files in /etc, pipe the list to grep, and grep will search that list for matches to "serv".

The output of grep can be long. You can find out which files contain some text, but you'll have to pipe the list through less:

grep tcp /etc/s* | less
Will grep /etc/s* (which is a lot of files) looking for "tcp". Some files you can't read, and so a few permission messages will print. That's ok.

Use the -c to get a count of matches in each file.

grep -c tcp /etc/s* | less

This prints lines like:

/etc/sensors.conf:0
/etc/services:262
/etc/sestatus.conf:0

We really are only interested in files that don't have zero matches, so we can use the -v switch to get lines which did not match (reversed logic).

grep -c tcp /etc/s* | grep -v :0
Search for matches against "tcp" and pipe that list to grep and search for lines that did not match ":0". Below is a transcript.

[mst3k@tull ~]$ grep -c tcp /etc/s* | grep -v :0
grep: /etc/securetty: Permission denied
grep: /etc/shadow: Permission denied
grep: /etc/shadow-: Permission denied
grep: /etc/sudoers: Permission denied
/etc/services:262
[mst3k@tull ~]$

It is common to use grep, ls, as wc, and find. find is complicated and deserves its own chapter. wc ("word count") will count bytes, words, and lines.

ls *.pl | wc -l
List all the Perl scripts and count them (by counting the lines in the output).

There are a few oddities at the command line. It can be tricky to list files with a leading dot (called "dot files") without listing other files. Deleting dot files requires a trick or two. Dealing with files that have strange characters (including spaces) in the file names also requires special tricks.

Please email comments or suggestions about this page to achs-secretary@virginia.edu.

Academic Computing Health Sciences
Box 800555
Charlottesville, VA 22908
(434) 982-4025 phone
(434) 982-4030 fax

© 2008 by the Rector and Visitors of the University of Virginia.

The information contained on the University of Virginia’s Department of Information Technology and Communication (ITC) website is provided as a public service with the understanding that ITC makes no representations or warranties, either expressed or implied, concerning the accuracy, completeness, reliability or suitability of the information, including warrantees of title, non-infringement of copyright or patent rights of others. These pages are expected to represent the University of Virginia community and the State of Virginia in a professional manner in accordance with the University of Virginia’s Computing Policies.