Unix Intro: Difference between revisions

From Redbrick Wiki
Jump to navigation Jump to search
Line 40: Line 40:


  cd ~
  cd ~
However, cd on its own should do the same thing.
cd


=== Your current/parent directory ===
=== Your current/parent directory ===

Revision as of 12:21, 3 March 2007

Beginning Unix

This is a primer for those of you who have no previous experience of a UNIX system (which is most of us :o) ) and will teach you the basics of changing your shell and using programs such as mail and news.

Using your shell

Ok, when you first start using Redbrick and you log on, you'll be looking at a screen which looks fairly plain with some basic instructions for reading mail, news, chat, etc. This is great when you're starting off, but after a while, you'll find that just staring at the screen can hurt your eyes, so you'll want to do other stuff. You'll then see why a lot of people type stuff when using computers. The way that most people prefer when they get used to it, is typing in commands in a 'shell'. This sounds really difficult, but you'll soon get into the hang of it and be heying your friends and going into chat and mailing your ma and so on :o)

Directory Structure in Unix

The root directory

Every file on Redbrick has what's known as a path. Basically, it's the location of the file on the system. The format in Unix is a little different to Windows, for example, on a Windows/DOS machine, an example would be:

c:\projects\essay\essay.txt

while on a Unix system like Redbrick this could be:

/home/member/bubble/project/essay/essay.txt

The difference here is that you don't actually specify drive letters. Everything begins at the root folder, which is denoted by a forwardslash (/). Everything then goes from that, with each subfolder being separated by another /, but don't get confused by the two. One last thing on this topic, you can change to the root folder by typing:

cd /

Your home directory

Your home directory is where you store all your files. On Redbrick, your home directory is:

/home/member/u/username/

where 'u' is the first lettter of the username. Committee members would find their home directory in:

/home/committe/username

That folder, and everything in it is yours, and you have control over it. You can add files, create subfolders and change permissions on your files. Your mail is stored there, as is everything else. To change into your directory, you use the cd command. One way of changing into your home directory is by typing

cd /home/member/firstletter/username

but there is a special character to represent your home directory, namely tilde (~). So a shorter version is:

cd ~

However, cd on its own should do the same thing.

cd

Your current/parent directory

There are lots of special characters in unix. You've seen two already, / for the root directory, and ~ for your home directory. Another one is simply '.'. What a fullstop represents is your current directory. You might not think this is useful, but it is, especially in commands like cp and mv for example, where you are copying or moving stuff into your current directory. It can also be expressed as ./, but they both mean the same thing.

Then there's what's known as your parent directory, the one just before your own. In the example, if you were in the folder:

/usr/local/bin

then your parent folder would be:

/usr/local

Your parent folder is denoted by '../' So, if you wanted to change into it, you simply use the command:

cd ../

Basic Unix commands

Say you wanted to create a file in your home directory called 'phonenumbers'. You need to use an editor. The editor on Redbrick that is the best to use when you're starting off, is nano. In fact, lots of experienced users still use it. So, to create your file, you would type:

nano phonenumbers

This will open up the nano editor, with a blank file called 'phonenumbers'. Now, just simply type in whatever you want in this file.... To save it you can hold down control and hit 'o' (when I say control-o, that's what I mean), then hit return to confirm the filename 'phonenumbers'. If you want to exit, then you don't have to do a control-o, you can use control-x. It will ask you if you want to save the changes, so you press 'y' and again hit return to confirm the filename.

Some more unix commands:

who

The command 'who' lists all the users on the system, what pts they're on, the time they logged on and the machine they logged on from.

ls

The command 'ls' simply lists all your files in the folder that you are in, One point, there can be 'hidden' files in a folder. Their names begin with a "." They aren't really hidden, it's just that ls won't see them, unless you tell it to look for them by using the command:

ls -a

This will show ALL files in that folder. Another 'argument' that you can pass ls, is the l option, which gives a detailed listing of the files, and you can specify more than one argument to ls, or any other command for that matter. The command:

ls -al

will give a detailed listing of all files in your current folder, including 'hidden' files.

cd

I've already covered this in a way, it changes your current folder or 'working directory' as is the proper name for it. Some examples:

cd /usr/local/bin

changes your working directory to /usr/local/bin

cd /

changes your working directory to the root directory

cd ../

changes to your parent directory

cd ~

changes to your home directory

mkdir

This command simply creates a directory. You can only create directories in directories that you have permission to write to, such as your home directory, or /tmp for example. Some examples:

mkdir project

creates a directory called 'project' in your current directory

mkdir ~/project

does the same, only puts it in your home directory, regardless of whether your home directory is your working directory or not

cp

The command cp is used to copy files around the place. There are two arguments you must pass to cp, the source file (the one you want to copy) and the target file (the file you want to copy it to). That sounds confusing but it's not really. When you copy a file, you can specify a directory as the target, that way the file will be copied with the same name into that directory. Some examples will show this better:

cp ~/project/intro.txt ~/

will copy intro.txt out of ~/project into your home directory

cp ~/project/intro.txt ~/introduction.txt

will make a copy of the same file in your home dir, only renamed to introduction.txt

cp ~/project/intro.txt ./

will copy the same file into your current working directory

You can't copy directories like this however, to copy a directory you must use the -R switch, which will copy all sub-directories of that directory as well, so an example would be:

cp -R ~/project/images ~/images

which will copy the directory 'images' out of 'project' to your home directory

mv

The command mv moves files between folders, and can move folders themselves. Its usage is very similar to that of cp. in fact it's the same, only it deletes the source file, leaving the target file. That is, if you mv one file into another folder, the new file will remain, while the old one is deleted, in effect, a 'move'. mv is also used to rename files. Some examples:

mv ~/hello.txt ~/bye.txt

renames hello.txt to bye.txt (in your home directory)

mv ~/bye.txt ~/project/hello.txt

renames bye.txt to hello.txt and moves it into the directory ~/project

mv ~/project ~/oldstuff/project

moves the directory ~/project into ~/oldstuff

rm

The command rm is used to delete files. Be careful with this command as once you remove a file, you've lost it unless you made a backup of it. The command is easy to use, an example is:

rm ~/hello.txt

to delete the file 'hello.txt' out of your home directory

pwd

One of the simpler commands, simply typing pwd at the prompt will display your current working directory. 'pwd' stands for 'print working directory'.

cat

This stands for concatenate. This command is really useful, if you have a text file you want to view without going into a text editor, you can simply type, for example:

cat ~/hello.txt

which will simply display the text file on the screen. If the text file is large, even more than a screen, then this can be annoying. This is where the command more comes in. 'Piping' the 'cat hello.txt' into it will cause the page to be displayed on screenful at a time, using return to go down one line at a time, or space to go down one screenful at a time. There is a command called less which does the same, but it wont bring you back to the command line after youve finished viewing the text file until you press 'q'. The command you need to type is

cat hello.txt | less

The '|' is called a pipe, got usually by holding down shift and \, and is used for putting the output of one command, in this case, 'cat hello.txt' into the input of another command, in this case, 'less'. There are lots of things you can do with cat, and redirection(using pipes etc..) but I'm not going to go into that here.

That's it....

There are more unix commands that you can imagine, the ones I have given here are basic ones, with a few basic examples for you to get started. To get more uses for any of the commands or any others, simply type:

man command-name

to get detailed instructions on that command. The best way to learn Unix is to experiment, just be careful when you're going around deleting stuff. :) This is just a primer for those of you starting off on Unix, but there are numerous tutorials on unix to be found on the web and you can always talk to Helpdesk if you have a problem or a question.

Links

Unix Advanced - a guide to some of the more advanced parts of Unix (if you're ready).

UNIXhelp for Users - Unix guide.