Hoffman2:Linux Tutorial

From Center for Cognitive Neuroscience
Revision as of 02:00, 14 March 2012 by Elau (talk | contribs) (Created page with "Back to all things Hoffman2 ==The Trifecta of Every Day Unix== Working with UNIX and UNIX like systems via the command line can be intimidating. The good news is a ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Back to all things Hoffman2

The Trifecta of Every Day Unix

Working with UNIX and UNIX like systems via the command line can be intimidating. The good news is a normal user only needs to have a firm understanding of three principle actions to efficiently use UNIX for their every day work.

  1. POSIX Permissions
  2. File System Navigation
  3. File & Shell Management
  4. Learning to Fish or The Man Pages

Each of these areas requires knowledge of a few small programs and how they behave. In the following sections, each program and utility is covered in detail. When appropriate, a link to the relevant wiki page on the topic is provided.

Each section is followed by an itemized list of the programs covered and some links to further information on that particular utility as well as the command to bring up the man page for the utility contained within parthesis.

Where appropriate, links to external tutorials deemed suitable and appropriate to the topic are provided rather than reproducing the work here.


POSIX Permissions

POSIX permissions are what determines who and to what degree users can access a file. The key terminology and function of the POSIX permission system is found on the UNIX Permissions wiki page.

List of Utilities Covered


File System Navigation

The following series of tutorials provide a very basic introduction to file system navigation on unix like systems without any assumptions of prior knowledge on the topics.

  • Listing files & directories, making directories, changing directories, the . and .. directories, pathnames, the "home" directories
  • Copying files, moving files, removing files and directories, displaying the contents of a file, searching the contents of a file

List of Utilities Covered

  • ls
  • mkdir
  • cd
  • pwd
  • cp
  • mv
  • rm
  • cat
  • less
  • head & tail
  • grep


File & Shell Management

This is where your career on a UNIX type system can be made or crippled. Sure you know how to move around, list files, find out where you are, and display the contents of files. But now you have to do something with those files. And let's face it, there are a whole lot of files.

It is highly recommended that the reader look over Tutorial Four on how to use wildcards for matching before preceding with this section.

The good news is, using the above utilties we just learned about we can accomplish almost anything we want to do using a very handy utility called find.

As the name might imply, find, well, finds things. What it finds is up to you. find has many, many options. All laid out in its man page. However, for most purposes only a few are needed. We'll cover those here.

A basic find command looks like

$ find /path/to/directory -name 'filename.txt'

This command looks at all files in /path/to/directory and in all directories therein for a file named 'filename.txt'.

Common Options

-type
Specifies the type of file we're looking for. e.g. text file, directory, link, etc.
-name
Specifies the name of the file. Case Sensitive
-iname
Specifies the name of the file. Case Insensitive
-or
Joins the precedeing and following terms by the boolean OR
-and
Joins the preceding and following terms by the boolean AND
-not
negates the next term. e.g. -not -empty means "is not empty"
-exec
excutes a shell command for each file found. The only 'trick' is to replace the actual file name with {} and end the command with a \;. This should become clear when reviewing the examples below.
-empty
The file or directory is empty.

We can combine the above options to preform complex searches on the file system and, even better, execute commands on those search terms. For a list of all options and their arguments, please see Find Man Page.

Examples

  1. Find all directories named 'tsplot' in the current directory
$ find . -name tsplot -type d 
  1. Find all empty directories in the directory /u/home9/foo/data
$ find /u/home9/foo/data -empty 
  1. Find all empty files or directories named 'tsplot' in the current directory
$ find . -name tsplot -or -type d -empty

Using commands we've already learned to perform actions on the above.

  1. Find all directories named 'tsplot' in the current directory and delete them using rm
$ find . -name tsplot -type d -exec rm -rf {} \;
  1. Find all empty directories in the directory /u/home9/foo/data and delete them
$ find . -type d -empty -exec rmdir {} \; # using rmdir for empty directories is safe as it will refuse to delete a directory with a file in it
  1. Find all empty files or directories named 'tsplot' in the current directory and delete them
$ find . -name tsplot -or -type d -empty -exec rm -rf {} \;

But we're not restricted to rm, of course, we can use any of the utilities we've learned about as the -exec argument.

  1. Find all files named design.fsf and look for a subject named 'foo'
$ find . -name design.fsf -type f -exec grep "foo" {} \;

With a little reading of the man page, we find a new option called -user, which surprisingly finds all files that belong to the specified user

  1. Find all files owned by user 'foo' and change their permissions
$ find . -type f -user foo -exec chmod -R ug+rwX {} \;


Learning to Fish or The Man Pages

However, possibly even more important is being able to find information on one's own. In the spirit of this, the instructional is concluded with a detailed coverage of the built-in unix manual pages. Virtually every program installed on a UNIX like system provied a manual page for reference. Even the most proficient of UNIX users could never retain the dizzying array of programs and their options in their working memory and with the built-in manual pages, they'll never need to.

The man pages (for manual) are the be all end all reference on UNIX systems. When reading a man page becomes natural, you know there is nothing you can't get done quickly and efficiently.

A Beginners Guide to man Pages is an excellent introduction into how to move around a man page easily and understand what it's telling you.