Tuesday, February 10, 2009

Unix Command

There are a couple of things that you need to keep in mind when using Unix. First off, everything is case sensitive in Unix. README, ReadMe, and readme are three different files that can all reside in the same directory. Second, you always use a ‘/’ not a ‘\’. This can take some getting use to if you are coming from a MS background. Also, while NT has one command shell (cmd), Unix has many. The one that you are probably going to want to use is bash. You can call this shell at any time by typing bash and hitting enter. Bash gives you some doskey like functions (like pushing up for previous commands) that you will find useful. The last side note is on the ‘|’ character. The pipe is very useful in Unix as it will let you use several small tools in conjunction to create complex tools. This doc is for Unix implementations built off of the SYSTEM V kernel (Solaris, AIX, HPUX). BSD derivatives may have different syntax.

Getting around

Commands:

ls = (dir) lists the contents of a directory
ls -al |more = this will display all files (including hidden) and there attributes. The |more will display the results one screen at a time.
pwd = displays your current directory
cd directory = change directory- it works like dos except that it requires a space between the command and first arg.
cd / = cd to root
cd .. = go up one directory
cd = cd by itself wil take you to the env defined $HOME dir
mkdir test = make directory test in the current dir
rmdir test = remove the directory test from the current dir (delete it)
rm testfile = delete the file testfile

Drives- Unix does not have logical drives (c: d: e:) it has mount points instead. I won’t go into mounting drives here, but I wanted you to know where to look for a cdrom. By default, a cdrom will be mounted in the root directory as a directory called cdrom (easy huh.) cd /cdrom should bring you to the cdrom from anywhere.


The File System and Permissions

This is the output from a ls -al |more in the root directory on a Solaris box:

# ls -al |more
total 51023
drwxr-xr-x 30 root root 1536 May 5 12:24 .
drwxr-xr-x 30 root root 1536 May 5 12:24 ..
-rw------- 1 root other 281 May 5 12:24 .Xauthority
-rw------- 1 root other 461 May 12 16:45 .bash_history
-rw-r--r-- 1 root other 231 Oct 25 1999 .bashrc
-rw------- 1 root root 1032 May 5 12:23 .cpr_config
drwxr-xr-x 13 root other 512 May 5 12:25 .dt
-rwxr-xr-x 1 root other 5111 Aug 30 1999 .dtprofile
-rw------- 1 root other 0 Apr 5 10:12 .lynx_cookies
drwxr-xr-x 11 root other 512 Nov 1 1999 .microsoft
drwxr-xr-x 2 root other 512 Sep 2 1999 .ssh
drwxr-xr-x 2 root other 512 Oct 4 1999 KeyContainer
-rw-r--r-- 1 root other 0 Feb 9 18:08 Office2000
drwxr-xr-x 2 root root 512 Aug 30 1999 TT_DB
-rw-r--r-- 1 root other 30 Apr 7 16:56 ap
lrwxrwxrwx 1 root root 9 Aug 30 1999 bin -> ./usr/bin
drwxr-xr-x 2 root nobody 512 Sep 28 1999 cdrom
-rw-r--r-- 1 root other 2506 Mar 28 12:45 common.policy
drwxrwxr-x 17 root sys 3584 May 5 12:23 dev
drwxrwxr-x 5 root sys 512 Aug 30 1999 devices
drwxr-xr-x 28 root sys 3072 May 5 12:23 etc
drwxrwxr-x 3 root sys 512 Nov 17 09:06 export
-rw-r--r-- 1 root other 1924 Apr 12 17:12 foo.cap
dr-xr-xr-x 1 root root 1 May 5 12:23 home
-rw-r--r-- 1 root other 1756 Apr 12 17:13 host
-rw-r--r-- 1 root other 189 Jan 6 14:55 iss_signing_authority_239.Pub
--More--

The first column is made up of 10 letters or -'s. The first one can be 'd' or 'l' (there are other letters that might appear here, but you shouldn't see them in your use of Unix.) 'd' means that the filename is a directory. 'l' means that the filename is a symbolic link (kind of like shortcuts in NT only better.) The other 9 places show the permissions on the file. 'r' denotes read access, 'w' denotes write access, and 'x' denotes the ability to execute a file. The first group (-rwx------) is the permissions for the owner of the file. The second (----rwx---) is for the primary group that the owner belongs to. The last set (-------rwx) is for everyone.

The third column is the owner of the file- root owns all of these files.

The fourth column is the group for which the group permissions apply.

Following this we have: file size in bytes; month, day, time last modified; and the file name.

Important notes on filenames-
anything preceded by a '.' is a hidden file (.Xauthority)
anything with a -> in it is a symbolic link. It will take the form filename -> ./dirpath/file_or_dir_pointed_to as I said before- Unix is case sensitive.

Changing Permissions

chmod oge filename = change permissions where o=owner g=group e=everyone (this isn't classic notation, but it makes more sense

Permissions on a file/dir in Unix are stored in a 24 bit variable. 8 bits are used for each of the three permission sets, so the letters above are replaced with the int value for that set (have no fear, examples will follow.) You have to dust off your binary math for this so I will make it easy for you. The possible values for these numbers range from 0 (---) to 7 (rwx). When the bits are set- r = 4, w = 2,and x = 1. You add the bit values together for the set to come up with the correct number. So, if you wanted full control for the owner/root, write/execute for the group, and read only for everyone else, you would type the following:

chmod 734 filename

The permissions would now look like this:

-rwx-wxr--

chmod 644 filename

would look like this:

-rw-r--r--

Executing commands


There are a few things about running commands in Unix that are very different from the MS world. In DOS, you know a file is executable based on it's file extension (.exe .com .bat ect.) Unix does not use file extensions to see if a file is executable- it looks at the permissions (see above.) In DOS, there are three ways to run an executable- by typing the file's name if it is in the path, by typing the full pathname to the file, or by typing the file's from the file's directory.

example (in NT):

doskey (from anywhere if c:\winnt\system32 is in your path)
c:\winnt\system32\doskey (from anywhere)
doskey (from \winnt\system32 even if it's not in the path)

Unix works a little differently. To run a command, it must be in your path or you must specify the path name(absolute or relative.)

example (Unix):

traceroute IP (from anywhere if /usr/sbin is in the path)
/usr/sbin/traceroute IP (from anywhere)
./traceroute (from /usr/sbin even if it is not in the path.)

No comments:

Post a Comment