Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the...

24
Unix on the Command Line: A Bag of Tricks Draft Version: Written by Prof. Leon King (not to be copied or distributed except by the author)

Transcript of Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the...

Page 1: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

Unix on the Command Line: A Bag of Tricks

Draft Version: Written by Prof. Leon King (not to be copied or distributed except by the author)

Page 2: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

Conventions

This is a draft text – any corrections or suggestion please forward to me at: [email protected]

When I use bolded text, this indicates something that should be literally typed in. When italics are

used, substitute an appropriate value, ie:

ssh [email protected] #This is a comment

Instead of yourId, type in your own userid

Everything appearing in the same line after a hash symbol (#) is a comment, which will have no effect.

You don’t have to type in comments when trying something out but they are useful for documenting

your assignments – use them to indicate what you are trying to accomplish, especially if the result is not

what your expect.

If text appears in brown it’s produced by the system – no need to type it in.

If you see 3 dots (ellipses) you don’t type it in. It means more of the same

touch file1 file2 file3 ... #create multiple files – as many as you’d like to

Page 3: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

Logging On to Unix

Your first priority is to make sure you can login. To login to the munro Unix server at Humber College we

use the free ssh (secure shell) communications program PuTTY which is selected from the Windows

start menu. PuTTy for Windows or Mac1 can be downloaded from http://putty.org. You may also

wish to try the program Bitvise which is free for personal but not institutional use.

Type in your userid followed by @munro.humber.ca and then click Open. You will then be prompted

for your password. When you type in a password Unix will not echo anything back at you. This is to

conceal the length of your password which could be used as a clue to break into your account.

1 If your personal computer is a Mac, underneath the Graphic User Interface (GUI) you are already running a version of Unix based on the BSD Mach Kernel. With few exceptions, every command we learn will work more or same on the Mac – you can simply open a Terminal window. The organization of directories on a Mac is significantly but not radically different. To connect to the Unix server, type in the command: ssh [email protected]

Page 4: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

If you were able to successfully login you will be presented with the command prompt:

bash-3.2% your command will be entered here

Do You Want to be a WIMP or a Technician?

Common end user graphic user interfaces (GUIs) use what is known as a WIMP interface: Windows,

Icons, Menus and Pointers though both have a command line similar to Unix. Unix also has a GUI

known as XWindows, with two main variants, Gnome and KDE, however these will not be part of this

course.

The advantage of a GUI is that everything you think you need to do as an end user is right there in front

of you. Usability guru Donald Norman calls this knowledge in the world. There is very little that you

have to remember, you click on a menu or right click on an object and a well designed GUI tells in a

word or two or by popping up a dialog what you can do.

In a command line you have to remember all the commands and options. Norman calls this knowledge

in your head. One advantage of the command line is that you can often accomplish more in less time.

Commands can also be grouped together in a script file. A script file then becomes just another

command. The ability to glue commands together like this is another major advantage of the command

line. If you are an end user a GUI is probably sufficient. It you are doing technical support and

administration it isn’t.

The original developers of Unix apparently weren`t very good at typing didn’t like typing, so Unix

commands are often very short – cp instead of copy, rm instead of remove. By the end of this course

you will have learned approximately 80 different commands and professionally you won’t be just a

WIMP.

Introducing the Command line

Commands usually have the following syntax:

bash-3.2% cmd -a arg1 -bc -d arg2 arg3 arg4 arg5 -e --longFlag arg6 arg7 ...

The command itself is a single word, no spaces, case sensitive and usually in lower case letters.

Flags, options and arguments are all optional. The letters preceded by a dash are known as short flags.

Some of these take an argument, others do not – which ones do and what they mean has to be learned.

The order of flags is irrelevant and a flag can appear anywhere in the command line. This is useful in

case you forget a flag as you can always type it in at the end of the line. Short flags can be grouped

together as long as only the last flag takes an argument. In the above example, -bc -d -e could have

been expressed as -c -b -ed or –dbec. Long flags are usually preceded by a double dash.

Optionally they can take arguments as well.

Page 5: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

Some of Unix’s older commands don’t follow these rules. We’ll note the exceptions as we go along.

Your First Commands

Try out the following commands and write a summary next to each one what they show:

# This is a comment (nothing should happen)

date

uptime

uname -a

echo “Is anybody there”

who

who -b #note the effect of the short flag -b which takes no argument

id

id king

who am I

tty

last

last $USER

last

ps

ps -aux

All of the above were simple commands that told you something about the system and about you as a

user and other users on the system. Now consider how these commands would be useful to you as a

Systems Administrator.

Page 6: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

Working With Files and Directories

Creating files is pretty easy:

bash-3.2% touch myFile otherFile aFile

Now lets look at them in the file system using the followling 3 commands: ls, ls -li, stat

bash-3.2% ls #ls stands for list subdirectory and shows (almost) all files in the current directory.

aFile myFile otherfile

Notice that the file names are automatically placed in alphabetic order. bash-3.2% ls -li #The -l flag means long - you get more information about the file #The i flag asks the system to display the file’s inode # - a unique id for each file #on the same hard drive total 0 2013645 -rw-r--r-- 1 king staff 0 Jan 10 14:13 aFile 2013642 -rw-r--r-- 1 king staff 0 Jan 10 14:13 myFile 2013643 -rw-r--r-- 1 king staff 0 Jan 10 14:13 otherFile The output is read as follows:

The first number is the inode #. They’re generated in sequence but they don’t have to be.

The total size in bytes of all files is 0 bytes.

The initial dash at the start of the line indicates the file type.

The next 9 characters indicate the file permissions. All three files are readable and writeable by

the owner and readable by other members of the owner’s group. More on file permissions in

the next section.

after the permissions is the hard link count. More on that coming up.

king is the owner of the file in each case

the files can be shared with the group staff who are only allowed to read the file

The file sizes are all zero

The files were last modified on Jan 10th at 2:13 pm

the final item on each line is the name of the file

Page 7: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

bash-3.2% stat myFile File: `myFile' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 821h/2081d Inode: 2013642 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 551/ king) Gid: ( 504/ staff) Access: 2015-01-10 14:13:07.000000000 -0500 Modify: 2015-01-10 14:13:07.000000000 -0500 Change: 2015-01-10 18:40:49.000000000 -0500 The stat (file status) command reveals everything about a file other its content. It goes beyond ls -li in that it tells us that the file occupies 0 blocks of storage, and that each block would take a minimum of 4K. The file permissions are shown both as symbols and as an octal number, and both the user name and the group name have equivalent and unique numbers. But what is especially interesting is that each file has 3 times assocated with it, which can be used to track usage. The Access time shows when the file was last used. The Modify time indicates when the content of the file was last changed. The Change time reflects when any of the above properties of the file were last changed. This name “Change time” was probably a bad idea as its easy to confuse it with the Modify time, , but the decision was made 45 years ago and we’re stuck with it. The vim Editor Background vi (short for visual) was an early screen editor for Unix developed by Bill Joy in 1976. Back in the old days there were no screen based terminals. People intereracted with computers either using punch cards or typewriter style devices known as teletypes. The standard editor for Unix was called ed2i. vi was built on top of ed and added many commands so that one could edit using a full screen, however the terminals back then had very little memory could only display ascii characters. In 1988 a developer created a clone of vi which he called vim (vi imitation or vi improved) and by 1992 he ported it to Unix3ii. There is also a GUI based version of vim that runs on both Unix and Windows called gvim. vim is an editor specifically developed for software developers and technicians. The initial learning curve is rather steep but at the end of the road you will be able to do far more than with a regular text based editor including directly viewing and editing binary files. Another popular editor of this kind is called emacs, which won’t be covered.

2 The Kochan text covers ed, not vim 3 A complete history of vim can be found on Bram’s website: http://moolenaar.net/vimstory.pdf If you like vim

and find it useful, send him a couple of dollars. Moolenar doesn’t make money from the sale of vim, but he does take donations which he forwards to help orphans in Uganda

Page 8: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

Using vim

vim has four modes of operation. When you start vim you are in command mode. You press the a key or a set of keys which allow you to move around, insert, delete, undo your changes, save your changes and quit the editor. Typing in the letter “i” moves you to insertion mode. (The word “ – INSERT --” appears at the bottom left the screen.) Insertion mode allows you to type information into your file. The up, down, right and left arrows work in insertion mode, but you can only move over to letters in your file. You can't move past the end of a line. To go back to command mode you press the <ESC> key. (There is no harm in hitting it twice – if you are already in command mode the <ESC> key does nothing.)

i is the command in command mode that puts you into insert mode.

<ESC> takes you back to command mode.

The 3rd mode of operation is called command line mode. Remember that vim is built on top of ed? By

entering a colon in command mode you can enter the older commands. An especially useful one is

:wq saves the file and exits back to Unix

The 4h mode is called visual mode. It allows you to treat text as a 2 dimensional block. More on that

later.

If you are not sure which mode you are in, the <esc> key will get you into command mode, and from

there you can get to any of the other 3 modes. Hitting <esc> more than once is not a problem – you’ll

still be in command mode anyway.

Command Mode:

h, l, j, k

dd, dw, d$, db, x

u, U

:w, :wq

i

Insertion Mode:

What you type

appears

in the file

i

Page 9: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

The command vimtutor takes you into vim and runs you through a tutorial. Enter the command and try

it out. Then try editing a file. Use ls and stat to look at the files when you are done.

bash-3.2% vimtutor #This takes you into the tutorial

Directories

Directories are the same idea as folders. To find out what directory you are in, type in the command:

bash-3.2% pwd

/home/staff/king

All user accounts are by default stored under the /home directory. /home has 2 main subdirectories, staff and students. Your files would be stored in the directory /home/students/yourId To create a directory the command is mkdir mkdir alpha beta gamma yourdir

To delete a directory the command is rmdir

rmdir beta #This only works if the directory is empty

Using ls -li you get listings that look like this:

2013644 drwxr-xr-x 2 king staff 4096 Jan 10 19:26 alpha

What`s different is as follows:

The letter d before the permissions means the file type is a directory, not a regular file

The size of the file is 4K or 1 block.

The hard link count is 2, not 1

To create a subdirectory at the same time as a directory, mkdir has the –p flag which creates any

parent directories if they don’t already exist.

bash-3.2% mkdir -p delta/force/one gamma/radiation/hulk

Finally to see the directories you’ve created, execute the following:

Page 10: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

bash-3.2% tree

.

|-- aFile

|-- alpha

|-- delta

| `-- force

| `-- one

|-- gamma

| `-- radiation

| `-- hulk

|-- myFile

`-- otherFile

Navigating the File System

This section is not yet written. It covers the cd command and describes important directories. See

Kochan Chapter 2 and your own in-class notes.

File Permissions

Basic Unix security is based on the idea of 3 categories of people: users, groups and everyone else

(others), and 3 types of permissions : read, write and execute. Probably 20% of the technical problems

you will have relating to files will have something to do with improperly set permissions so its an

important topic to master.

Lets say you wander by someone’s terminal and want to find out who left themselves logged in. Simply

type in the command:

bash-3.2% id

uid=551(king) gid=504(staff) groups=504(staff),2331(sharks),2332(minnows),2333(dolphins)

In the above case my userid is king, my primary group is staff and I belong to 3 other groups: sharks,

minnows and dolphins. There are also unique numbers associated with each user and a unique

number associated with each group, but these aren’t significant at this point in time.

Page 11: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

You can also find out which groups someone else belongs to by typing in: id userid

Every file belongs to a user and group:

bash-3.2% ls -l file1 file2 file3

-rw-r--r-- 1 king staff 0 Jan 11 20:37 file1

-rw-rw---- 1 king staff 3195 Jan 11 20:38 file2

-rwxr--r-x 1 king staff 154 Jan 11 20:38 file3

Recall that the leading dash refers to the file type – all 3 files are regular files. The next 9 characters are

read in groups of 3. The first set of three are permissions related to the user, the person who owns the

file. As king, the owner, has read and write permissions for all 3. file3 has the execute permission set,

meaning its (probably) a program of some kind and, as the owner of the file, king is allowed to run the

program.

The files also belong to the group staff which refers to other employees of the college. All members of

staff are allowed to read each of the files, however no member of staff can write or execute any file,

including file3 as these permissions are turned off.

The last 3 permissions refer to anyone who is not the file’s owner or the file’s group. So members of

another group sharks or accounting can read and edit file3, however they can’t do anything with file2,

not even read it. If they try they will get and error message.

The idea of groups is that files are associated with either a project or a department. People will have

personal files that they want no-one else to see or use. They’ll also be working in groups on various

projects where files are shared. In some cases they’ll want to share the files with everyone, but

perhaps only give read or execute privilge – you may not want other people to modify your file, but

you’d like them to see the content. Or, in a data entry situation, you may want others to add data to

the file, but not see what others have written so you take away their read privilege. (In such cases they

won’t be able to review what they added either.)

There is a special individual (or group of individuals) who are members of group #0 – the root user, who

is the systems administrator who is not affected by any of these privileges. Root privilege is very

dangerous, even for the sysadmin. If this is your job you are advised to use a regular account and only

briefly switch over to your root account on an as needed basis.

What if you have a project that needs file access to more than one group? The short answer is to ask

the sysadmin to create an new group and add everyone who needs it to that group.4

4 A better answer is to use a more advanced form of Unix security called ACL (access control lists), which is also

available on routers and other types of systems.

Page 12: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

Changing the group of a file

The chgrp command is used to alter the group ownership of a file

bash-3.2% chgrp sharks file1 file3 ... #any number of files can follow

bash-3.2% ls -l file1 file2 file3

-rw-r--r-- 1 king sharks 0 Jan 11 20:37 file1

-rw-rw---- 1 king staff 3195 Jan 11 20:38 file2

-rwxr--r-x 1 king sharks 154 Jan 11 20:38 file3

Note the change in group ownship. The only other difference would show up if you used stat to look at

the change time - group ownership is a property of a file and change time reflects the change of any

property.

By default all files that you create belong to your login group. You can only change the group ownership

of a file to a group you you belong to. You can’t give your file away to another user – only root can do

this using the chown (change owner) command. However if they can read the file they could make a

copy of it and the new file would belong to them. They could then change the permissions on the copy,

not the original which they could then write to or execute.

The newgrp command allows you to change your current group to any of the groups you are a member

of. Any files you now create will belong to the new group and the id command will now show that

group as your primary group.

bash-3.2% newgrp minnows

bash-3.2% touch file4

bash-3.2% ls -l file4

-rw-r--r-- 1 king minnows 0 Jan 11 21:32 file4

It’s also possible to set up passwords for groups, in which case you would be prompted for a secondary

password, which is a good security practices, however we haven’t set this up in the college.

Sometimes people will have more than one account, or you want to loan your terminal to someone for a

couple of minutes. The su (substitute user) command allows you to do this:

bash-3.2% su bakerk

Password:

To return to your account type in the command: exit which logs the other user off.

Page 13: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

chmod: Changing Permissions symbolically

chmod uses the following symbols: ugoa+-=rwx and , (comma) After each command assume that

ls –l file1 is run:

To set permissions for members of your group : chmod g=rw file1 ....

-rw-rw---- 1 king sharks 0 Jan 11 20:37 file1

To set permissions for the user: chmod u=rx file1 file2 file3 ....

-rwxrw---- 1 king sharks 0 Jan 11 20:37 file1

To set permissions for the user and others: chmod ou=x file1 ...

---xrw---x 1 king sharks 0 Jan 11 20:37 file1

To set permissions for all: chmod a=r file1 ...

-r- - r-- r - - 1 king sharks 0 Jan 11 20:37 file1

You can mix and match ugoa in any order. After the equal sign you can mix rwx in any order.

To set different permissions for each: chmod u=r,g=x,o=w file1 ....

-r- - - - x - w - 1 king sharks 0 Jan 11 20:37 file1

To turn off all permissions: chmod ug= file1 ....

- - - - --- - - - 1 king sharks 0 Jan 11 20:37 file1

Note that the command will reset all the permissions for the specified category of user, but leave the

other categories intact.

You can also turn selected privileges on or off, which doesn’t affect other privileges.

Turn on read and execute for file1: chmod ug+rx file1

-r- x r-x - - - 1 king sharks 0 Jan 11 20:37 file1

Turn off read for user and group: chmod ug-r file1

- - - x - -- r - - 1 king sharks 0 Jan 11 20:37 file1

Turn on read for everyone, execute for others and turn off write for the users: chmod a+r,o+x,u-w file1

- - - x - -- - -x 1 king sharks 0 Jan 11 20:37 file1

Page 14: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

chmod: Changing Permissions Using Octal Notation

Internally permissions are represented by 9 bits5. To explain it, lets create a lookup table:

Binary Octal Privilege 000 0 none

001 1 - - x

010 2 - w -

011 3 - w x 100 4 r - -

101 5 r - x

110 6 r w -

111 7 r w x

We can reset a file’s permission using 3 octal digits:

bash-3.2% chmod 711 file1

-rwx--x--x 1 king sharks 0 Jan 11 20:37 file1

The first digit represents the user. Looking up octal 7 we see that all 3 permissions are turned on. The

user can read, write and execute the file.

bash-3.2% chmod 213 file1

--w- --x-wx 1 king sharks 0 Jan 11 20:37 file1

Again we look up the codes 2, 1 and 3 (or translate it to binary) to see which bits are on or off. A bit of

practice an it becomes 2nd nature, or if you understand the principle, you can easily recreate the table

and look it up. You might also remember that the stat command shows both the octal and the symbolic

version of permissions.

Finally, sometimes you don’t care what permissions are set for a file – you just want to copy the

permissions of an existing file. This is your first example of a long flag – it’s preceded by 2 dashes, not

once. In this case, whatever privilege file1 has, it’s assigned to file2 and all the files that follow.

chmod --reference=file1 file2 ....

5 To be honest, it’s 12 bits, but the extra 3 bits is an advanced topic that won’t be covered at this time.

Page 15: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

Permissions and Directories

Some explanation is necessary to explain how permissions work with directories. A directory is a just

like a file. When you create a directory, you the user will have read, write and execute priviliges, and

everyone else’s privileges will be turned off.

Try turning off read privilege for yourself on the home directory:

bash-3.2% chmod u-r .

bash-3.2% ls -l

ls: .: Permission denied

The conclusion is that you need read permission to see the files names in your directory. However you

can still look at, create or edit any file, even use ls on it ....as long as you remember what it is called.

bash-3.2% touch abc.dat

bash-3.2% ls -l abc.dat

bash-3.2% stat abc.dat

bash-3.2% vim abc.dat

Note of these will give you any problem.

Now turn off write privilege for yourself on your home directory. You can work with files, but you

won’t be able to create or delete a file as this changes the contents of the directory.

But what about the execute privilege? Directories can’t be programs so execute means navigable.

Turn off execute privilege for your home directory:

bash-3.2% chmod u=rw .

Even though read and write are turned on you can’t do anything that involves using the directory – in

each case you will get the message: Permission denied. You can’t create, edit, delete or rename a file,

change to a subdirectory or refer to it. You can’t use cd to navigate to the directory or a subdirectory of

your home directory because all of these implicitly require the system to use the directory to find the

file.

Now turn the navigate privilege back on:

bash-3.2% chmod u=rx $HOME #we use $HOME in case you navigated away from the directory.

#If you log off with navigate turned of on your home directory, you

# you will get the message: Access denied . You need to ask ITS to

#turn the privilege back on for you.

Page 16: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

Copying, Renaming and Deleting Files

The cp command is used to copy files:

cp myLetter.txt newCopyOfMyLetter.txt -pvi #Note that the flags can go anywhere

The -p flag tells Unix to copy the permissions of the file as well. Otherwise the new file will have the

default permissions of your session.

The -i flag tells Unix to prompt and give a warning if the destination file already exists. Normally the

command will fail with an error message if this is the case.

The -f flag means force the copy – copy over a file if it already exists.

The -v flag means verbose. When the copy takes place it gives the full directory path of the source and

target files.

You can also copy multiple files at once into a directory:

cp -v file1 file2 file3 .... gamma

To rename a file: mv file1 anotherName #rename is a similar command

Like the cp command, mv has the flags -i -v and –f which have the same effect.

To delete a file: rm file1 file2 file3 .... #rm means remove

Again the same flags -i -v and –f apply, but -f means not to display an error message if the file doesn’t

exists. the command rm -r myDirectory will remove not only the directory, but its contents and ll

subdirectorys - it’s rather powerful and dangerous – and there is no undo command available that can

recover any lost files. Use it with extreme caution!!!! Use rm -ri instead as it will prompt you each

time to make sure you know what your are doing.

Page 17: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

Creating File Aliases

Hard Links

An alias is NOT a copy of a file, it is a secondary reference to it either in the same directory or

another directory. That way different users can access the same document or program using

different names without the storage overhead of a copy. A 1 megabyte file distributed to 1000

people would take up a gigabyte of storage, which doesn’t happen with an alias. Another advantage

is that if a change is made to the file all users will see the changes. If you distributed copies of the

file then you would have to send everyone a new copy. With an alias you are always up to date.

There are two kinds of alias. The first is called a hard link. Hard links all have the same inode #.

the syntax is as follows:

bash-3.2% ln aFile newFile

bash-3.2% ls -i l #verify results

2013645 -rw-r--r—2 king staff 0 Jan 10 14:13 aFile

2013645 -rw-r--r—2 king staff 0 Jan 10 14:13 newFile

If you use vim to edit one file, then look into the other you will find that the contents are identical. Not

only is the inode # and all other properties of the file the same, the link count (following the

permissions) has been increased to 2.

bash-3.2% ln ~king/.vimrc ~/.vimrc #Create a link to a resource control file in king’s directory

bash-3.2% ls -li ~king/.vimrc ~/.vimrc #verify - both have the same inode #s and permissions

When you execute the above (and you only have to do it once) you are linking to a file that contains

several macros and function keys that I’ve created for your use. For example if you use vim to create a

C Program, a PHP program or an HTML file you wil find that strings, keywords and variables will now be

colour codes – which will help which will help detect their correct use and determine unterminated

comments, tags and strings.

The mechanism behind it is as follows. Unix directories simple files consisting of two pieces of

information for other files – the inode # and the file name. You can change the file name – the only

thing that happens is that you change the entry in the directory.

What the inode# (information node) does is that it points into a block of memory on the disk known as

the superblock where each entry contains the rest of the file information about the file – its

permissions, owership, link count and access, modification and change property times, but not the file

name which is in the directory. The actual layout of this data store is not known to the end user and is

largely irrelevant, and in fact there are several different types of file system where the layout is

different.

Page 18: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

Superblock

1000 1001 1002 1003 1004 1005 1006 1007

DirectoryA File Property and Data Region

DirectoryB

In the above diagram both DirectoryA/myFile and DirectoryB/aliasToMyFile are both defined as

inode# 1003, which in turn points to a block of memory on the disk. They are therefore the same file.

Also note that in DirectoryB there is an entry . (dot) - all directories point to themselves. There’s also

an entry .. (dot dot) that refers to the parent directory. All directories, even the root directory, point to

a parent directory. Note that the parent directory .. of DirectoryB is the same file as . – DirectoryA

Files that start with a dot are called hidden files because they are not normally shown when you run the

ls command, however they are shown if you add the –a (all) option. This is not a security measure6.

Files that start with a dot are usually support files created by the system or some utility. They are

hidden as a convenience so that the user normally sees only the files that they have created.

bash-3.2% ls -lai

2013639 drwxr-xr-x 5 king staff 4096 Jan 12 08:51 .

1553441 drwxr-x--x 76 king staff 32768 Jan 12 08:50 ..

(more ordinary files follow)

6 Hiding information as opposed to encrypting it or denying access is called security by obscurity. Generally its a

bad idea. Like hiding your door key behind the garage door, eventually someone will find it.

inode# File Name

1000 . 990 ..

1003 myFile

1004 orders.txt 1007 January.report

1005 DirectoryB

inode# File Name

1005 .

1000 .. 1002 personnel.note

1006 anything.txt

1003 aliasToMyFile

Page 19: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

Files can be several gigabytes in size, but changing the name or creating a hardlink will be nearly

instantaneous because only the directory entry is changed.

A file may have any number of hard links. Any file, including the original name can be deleted but as

long as one hard link to the file exists, the file remains on the system. When the link count goes to

zero, the file is removed from the file system.

A version of the rm command is unlink, however unlink will only remove one file and it takes no flags.

Symbolic links

In the following example we create 2 symbolic links. In the 2nd case we use the full absolute path of the

file, but the effect will be the same.

bash-3.2% ln -s aFile symbolicLink #Note the flag -s

bash-3.2% ln -s /home/staff/king/d1/aFile other-Slink #Note the flag -s

bash-3.2% ls -li aFile symbolicLink other-Slink

2013654 -rw-r--r-- 1 king staff 0 Jan 11 16:37 aFile

2013646 lrwxrwxrwx 1 king staff 5 Jan 11 16:39 symbolicLink -> aFile

2013655 lrwxrwxrwx 1 king staff 22 Jan 11 16:49 symbolicLink2 -> /home/staff/king/aFile

When you try this out you should take note of several differences. The first is that the inode numbers

are different. The second is that the file type marker is l for symbolic link. Next, all the permissions

are turned on - in fact what will apply are the permissions of the original file. Fourth, the link count for

the new file is set to 1. Next the file size and the last accessed time is different. And finally after the

file name is an arrow indicating the name of the file that the alias points to.

The secret is the file sizes. What is stored in the file is the name of the file being pointed to. Inside

symbolicLink is 5 letters: aFile, the file name in the current directory. symbolicLink2 contains 22

characters, the same number of letters contained in /home/staff/king/aFile. (Use a different path and

you’ll get a slightly different result.

So why two mechanisms? Hard links have 2 restrictions. The first is that a hard link is unique, but only

to the hard drive that it is on. Symbolic links can refer to other hard drives. When hard links were

designed into Unix hard drives cost thousands of dollars - the expectation was that you could afford at

most one. The 2nd restriction is more recent – as a user you are not allowed to create a hard link to a

directory, however the system does this all the time with . (dot) and .. (dot dot). I’ve never been

satisfied with the reason given so I won’t go into it. Symbolic links can point to directories.

Page 20: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

Symbolic have one additonal advantage and disadvantage. An advantage symbolic link can point to a

file that doesn’t exist yet. You can create a link and then create the file later. If that’s the case the link

will be hilited in red when you run ls. A disadvantage is that if the original file is deleted, all the

symbolic links will point into empty space – if you attempt to use them you’ll get a ‘file not found’ error.

With hard links, if you have any hard link reference to the file it’s still around.

Other Details

The –f flag is used to force the creation of a symbolic or hard link – if the designated link file already

exists, using -f will force it to be deleted and replaced.

The -i flag makes this interactive. If the new link file doesn’t exist, there’s no different. If it does, it

prompts:

ln -i aFile cFile

ln: replace `cFile'? y

The –v flag means verbose.

ln -i aFile cFile -v

ln: replace `cFile'? y

create hard link `cFile' to `aFile'

An interesting aspect of this is that all directories have a minimum hard link count of 2. This is because

every single directy has 2 references to it. The first is its entry in its parent directory. For example, for

a home directory /home/students/abramsj , the directory abramsj is an entry in the students

directory. The 2nd hard link is because every directory has an entry . (dot) - which also refers to the

directory.

Similarly, every directory including the root directory has a reference .. (dot dot), which is a reference

to it’s parent directory. So when you add a subdirectory the link count of the direct parent will increase

by 1.

Page 21: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not
Page 22: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

Appendix: How to Use Humber’s vim Macros

Macros allow you to translate a few keystrokes into several commands. The following were developed

by Prof. Leon King a couple of aeons ago. Some are useful, others are just cute.

The following functions are mapped onto function keys:

F1 - Brings up the main vim help screen. This is standard for vim.

F2 – Saves the current file. Identical to :w

F3 – Compiles the current file as a C file w/o leaving the editor.

F4 – Runs the executable of the current C file w/o leaving the editor

F7 - Starts a bash subshell when you are in vim. To show that you are in a subshell, type in the ps

command. To exit the subshell and return to vim, type in exit

F9 – turn off hilighting in search

The following abbreviations expand to larger strings

DATE inserts the current date in the file

fori for(i=0;i<10;i++) { }

HC Humber College

IBM International Business Machines

INTER CENG 411 Unix Internals

MYSUB void sub1{}

PR fprintf(stdout,”Hello World\n”);

psvm Generates a simple Java “Hello World” program.

T154 Intro to Unix and the Internet

The following abbreviations expand to multiple lines of text:

BHEAD Generates a set of comments showing the current date, the user name and the course.

To detect the course you need to be in a directory with any of NEST, TECH or CENG in

the name.

FHEAD Does the same as BHEAD only in a format suitable for C Programming

HTML generates a standard HTML web page framework

Page 23: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not

SW generates a C language switch statement block

TAB generates an HTML table

WEBRW generates a row in an HTML table

WHO runs the who command and inserts the output in the file

Any of the above can be undone by typing in the vim undo command u

A minor annoyance is that you cannot directly type in any of the above strings without it being replaced.

However you can either type it the last character with a 4 second delay, or indirectly type it in, ie: type

in IB M and then go back and delete the space between the B and the M.

The following additional capabilities are enabled as well:

Automatic file backup. Every time you edit a file a version with .bak is saved containing the previous

contents.

Keywords in different programming languages are hilited in different colours.

When you do a search using / or ?, the text is immediately hilighted

Line numbers are automatically shown - useful for debugging or documenting errors.

Tabs are set to 4 character inserts.

How to set this up:

The secret is in a special resource controll file called .vimrc in your home directory which is loaded

every time you start the vim editor. To make these macros and capabilities available you need to issue

this command once and once only:

ln ~/.vimrc ~king/.vimrc

If you need to establish this file on a separate install such as a virtual linux machine, copy the file over.

Most students will simply use these macros as given. If you are interested in writing your own you

should consult the vim manual.

Page 24: Unix on the Command Line: A Bag of Tricks Draft Version ...munro.humber.ca/~king/TECH154/Unix on the Command Line.pdf · for your password. When you type in a password Unix will not