[FAQ] - UNIX basics for newbies

AdmiralAK

Simply Daemonic
OK, here is a small "how to" for knowing how to do a few basic things in UNIX (specifically the terminal in OS X).

Now when you open you your terminal you probably see something like:

Welcome to Darwin!
[hostname:~] your_username%

--> for the purposes of this HOW to we will use this:
Welcome to Darwin!
[admiralty:~] admiral%
<--


Now you might wonder how to so stuff in the terminal.
This is where the HOW-TO comes in handy.
(NOTE: I will do one command per post )

Lets start off with a command that lets you see what exaclty you have in teh directory you are in!

This command is the ls command.
The ls command could be thought of as "list" (list what's here). Doing the ls gives you something like this:

Code:
[admiralty:~] admiral% ls
Desktop   Library   Music     Public    admiralty
Documents Movies    Pictures  Sites
[admiralty:~] admiral%

What is in sylver color are the contents of where I am now (which happens to be my home directory).

------
There are special "flags" that go along with the LS command which "format" the output. Three useful ones are the following:

Code:
[admiralty:~] admiral% ls -a
.                   .FBCIndex           Documents           Pictures
..                  .FBCLockFolder      Library             Public
.CFUserTextEncoding .Trash              Movies              Sites
.DS_Store           Desktop             Music               admiralty
[admiralty:~] admiral%
the -a flag tells the ls command to show you EVERYTHING, yes!, even the invisible files (that start with a period (.) )

-----

There also exists the -l command which "lists" everything in a row. Lets examine it:

Code:
[admiralty:~] admiral% ls -l
total 0
drwx------   4 admiral  staff  264 Oct  4 19:29 Desktop
drwx------   5 admiral  staff  264 Oct  4 19:29 Documents
drwx------  22 admiral  staff  704 Oct  7 15:10 Library
drwx------   2 admiral  staff  264 Oct  4 19:30 Movies
drwx------   2 admiral  staff  264 Oct  4 19:30 Music
drwx------   2 admiral  staff  264 Oct  4 19:30 Pictures
drwxr-xr-x   3 admiral  staff  264 Aug 21 13:34 Public
drwxr-xr-x   4 admiral  staff  264 Aug 21 13:34 Sites
drwxr-xr-x   9 admiral  staff  264 Oct  4 19:47 admiralty
[admiralty:~] admiral%
Now you might be wondering what drwxrwxrwx is and all that stuff that is above.

--> continues bellow
 

AdmiralAK

Simply Daemonic
The first column of the above tells you what the "permissions" are.

d = directory
r = read
w = write
x = execute

now this is how to read drwxrwxrwx

file type owner (you) permissions group perms other perms
d rwx rwx rwx

Lets say I am another guy (I fall under others) and I have r-x this means that I only have read and execute permiossions. If I have --- means that I have no permissions. So whenever someone doesnt have a permiossion to do something the "-" appears where that permission would exist.

The 3rd column: Indicates the user who created that document
The 4th column: Indicates that user's group
The 6th, 7th and 8th columns indicate the date and time that file was last modified
and the 9th column indicates the file name

OK! Information overload, right ? well, this was a brief explantation, we'll take it easy from now on, I promise :)

----

Now the last LS flag for this HOW-TO, the -F flag

Code:
[admiralty:~] admiral% ls -F
Desktop/   Library/   Music/     Public/    admiralty/
Documents/ Movies/    Pictures/  Sites/
[admiralty:~] admiral%
The -F flag tells the LS command to also tell you what kind of files exist. Directories are denoted with a slash, so Movie/ is a folder called movies.


Now you know what is in your directory and to somewhat decipher what it is.
 

AdmiralAK

Simply Daemonic
well... so you want to change to another folder eh ?

The good thing is that it is simple!
there is only one command, cd .

Now. lets say we are in the same directory as above, and we want to go into Documents/ ... what do we do ?

we do the command cd documents...and pouf! we're there!
here is what it looks like:

Code:
[admiralty:~] admiral% cd Documents
[admiralty:~/Documents] admiral% ls
Above we see that we changed directory to Documents, and we can verify that we've changed to "Documents" because "Documents" has been added to our prompt! Cool eh ?:cool: (No I am not canadian :p)

---

Now lets say I wanna go back... it's also simple!
All you have to do is type
cd .. (cd space dot dot)

Code:
[admiralty:~/Documents] admiral% cd ..
[admiralty:~] admiral%
and we can verify that we actually moved back to the previous directory by looking at out prompt!


----

Now finally... lets say you want to go back to your home directory, but you are God knows where in your computer and dont know exactly WHERE your home directory is.

Lets say you are in:
Code:
admiralty:some_folder/some_Other_Folder/Some_smaller_folder/
and you want to go home. Well. The good unix folks thought about this one and they did this:

at the prompt just type cd and hit return. You will be back in your home directory :D
 

AdmiralAK

Simply Daemonic
OK, ready to move one step further ?
cool!

now to make a folder...its quite simple!

We use to mkdir command.
The format is this
mkdir desired_name

so if we want to make a folder called "job" we do this at the prompt:
mkdir job and hit return

Code:
[admiralty:~/pictures] admiral% mkdir job
[admiralty:~/pictures] admiral% ls
job
[admiralty:~/pictures] admiral%
To verify that it was actually made I did an "ls" to see teh contents of that directory. (NOTE: directory and folder are interchangeable terms)

-----

So..now how about deleting a few things ?

We do this with the rm command.

To delete a single file you do this

Code:
[admiralty:~/pictures] admiral% ls
job       text.txt
[admiralty:~/pictures] admiral% rm text.txt
[admiralty:~/pictures] admiral% ls
job
[admiralty:~/pictures] admiral%
Explantation:
I did an LS to see what I had in my folder. I had the folder named "job" and a text file called "text.txt".
I did the rm command followed by the file name I wanted to delete. I hit return.
The I did another LS to verify that I deleted it.


-----
To remove a directory you use the rm command with the flag -r (r stands for recursive but we wont get into that now)

here is what we have then:

Code:
[admiralty:~/pictures] admiral% ls
job
[admiralty:~/pictures] admiral% rm -r job
[admiralty:~/pictures] admiral% ls
[admiralty:~/pictures] admiral%
Explanation:
I did an LS to see what I had in my folder,
I had a folder called "job"
I did the command rm -r with the folder I wanted to delete (rm -r job)
and to check that it was deleted, I did an LS.


And that is how you delete! :)
 

AdmiralAK

Simply Daemonic
Ahhh renaming.
For that we use the mv command ("move" command).
It is done like this:
mv old_name new_name

The old_name is the file you have, the new_name is the name you want to give it.

lets take a look at an example:

Code:
[admiralty:~/pictures] admiral% mkdir jow
[admiralty:~/pictures] admiral% ls
jow
[admiralty:~/pictures] admiral% mv jow joe
[admiralty:~/pictures] admiral% ls
joe
[admiralty:~/pictures] admiral%
Explanation:
I made a directory called "jow" ... of course I made a typo!
To correct this is a simple matter with the MV command.
I just do mv jow (the file I made) joe (the new name I want to give it).
To make sure I did what I wanted it to do, I did an LS command, and I see that it worked!


---
How do you copy something ?

Simple! Use the command called cp (for CoPy).
The syntax of this command is similar to the MV command

cp source destination
where source is the source file, and destination is the destination file.

Lets say I want to duplicate my "AK" text file so that I have "AK" and "AK1":

Code:
[admiralty:~/pictures] admiral% ls
AK  joe
[admiralty:~/pictures] admiral% cp AK AK1
[admiralty:~/pictures] admiral% ls
AK  AK1 joe
[admiralty:~/pictures] admiral%
Explanation:
I did an LS to see what I had, and I DID have the AK file,
did the CP command as cp AK AK1 , where AK was my original file and AK1 is my duplicate "cloned" file.
Then I did an LS to double check and actually see that I did what I wanted to do



And that ladies and gents is how you copy and rename :)
 

AdmiralAK

Simply Daemonic
Finally 2 last commands :

the pwd ( Print Working Directory)
and the date commands.

pwd is nice if you dont know where you are.
Lets say you have been toying around with UNIX for some time now and you have gone to a directory but dont know where exaclty it is! pwd fixes this by outputting where you are.

Lets say I want to find where exaclty my home directory is,

Code:
[admiralty:~] admiral% pwd
/Users/admiral
[admiralty:~] admiral%
pwd tells me that my home directory is in the "users" folder. (this is a simple example, for now pwd might not come in handy but it the future it will :) )


And finally the date command.
It tells you the date, time, day and year , useful if you want to know the time and date ;)

sample:
Code:
[admiralty:~] admiral% date
Sun Oct  7 16:51:31 EDT 2001
[admiralty:~] admiral%
As you may notice the time is in the 24 hours format, and it has the Hours : minutes : seconds format as well.


Well, these are the most basic UNIX commands that one needs to survive :)



Admiral
 

Freestyler

Registered
Nice work - now what if i would like to copy all the files in a directory to another directory -
say the contents of /users/freestlyer/documents
to /users/freestlyer/archive/documents

both paths exist.


now, say that the contents of the archive/documents folder are a mix of .doc file and .xls files - and ive just made two sub directories
within /archive/documents

being

/users/freestlyer/archive/documents/xls
/users/freestlyer/archive/documents/doc

-In Dos I might put my pointer at /user/freestlyer
and go:
move c:\users\freestlyer\documents\*.* /users/freestlyer/archive/documents/*.*

or selectively sort from there with

c:\users\freestlyer\documents\*.doc <and so on and so forth>

got any tips?

Cheers

FreeStyler
 

baur

Registered
I realize this is an old thread, but I'll post something since I've seen the question of how to move directories and/or multiple files a couple of times.

To copy an entire directory tree, you can use the -R switch. For example:

% cp -R documents/ new_location/

OR

% cp -Rp documents/ new_location/

The first one will copy the directory and all sub-directories. The second one will attempt to preserve as many file attributes as it can (ownership, permissions, etc.)

To copy a number of files, you can use wildcards (similar to DOS, not the same).

% cp documents/*.doc new_location/

I didn't put the -R this time, since I'm assuming that *.doc will only match files. You can specify more than one source as well:

% cp documents/*.doc documents/*.xls new_location/

This will copy all the .doc and .xls files (and nothing else) into the directory new_location.

NOTE: if the destination is not a directory and only one source is listed, it will copy the file to a new filename. For example, to make a backup of a file:

% cp something.doc something-backup.doc

Hope that helps...

(use the 'man' command to get more info on anything here!)
 

Freestyler

Registered
Nice work!, ill take a look at your commands : thanks Braddah

FreeStyler
 

macfreak88

Bring me Tiger!
I thought i newer learn Unix but it wasn´t so hard...i love those commands can you give me some more? or send me a text file with some cammands...Thanx anyway
 

nkuvu

Gone
One other thing to add. At least, only one thing I can think of right now (since someone brought it up in another thread).

Let's say you have a file named "This is my file". And you want to delete it. If you type rm This is my file you get the following errors (or you delete the wrong file if one of these does exist!):
Code:
rm: This: No such file or directory
rm: is: No such file or directory
rm: my: No such file or directory
rm: file: No such file or directory
There are three easy ways to remove the pesky file.

One. Type "rm " (that's with the trailing space). Find the file in the Finder, and drag the file onto the Terminal window. You'll get the full path to the file added to the command line. So your command may look like rm /Users/nkuvu/Testing/This\ is\ my\ file. Press enter. *poof*

Two. Type rm T and press Tab. The operating system will automatically try to complete the filename or command. If there is only one file that begins with T, the full filename will be completed. If there is another file that begins with T, the OS will complete as much as it can and let you type the distinguishing character. For example, if you have two files "This is my file" and "This is a frog", the OS will fill in "This\ is\ ". You can press 'm' and then tab again, and the OS will complete the full filename. Press enter. *poof*

Three. Type rm 'This is my file'. Press enter. *poof* (The quotes tell the operating system that you want to ignore the spaces) Double quotes also work for this.

If you want, you can type out the whole thing by yourself, putting a \ before every space. But the other ways are a lot quicker.

This isn't limited to removing files -- any time you have folders or files with spaces in the name any of these methods can be used to complete the command.
 

macfreak88

Bring me Tiger!
I knew the two first ones but the qoutes thing was new it a lot eaysier than put a \ and a space...=-)
 

sao

member
Sorry if it was posted before, but I thought useful to add that when using rm is better to make an alias like this :

alias rm rm -i

or better still :

alias askrm rm -i

Cheers...
 

96.9

Registered
There is a new book out called learning Unix for Mac OS X .

Here is a link

http://safari.oreilly.com/main.asp?bookname=lunixmacosx

OS X should ship with a book like Free BSD . The info online is incomplete and more is needed I have found due to small changes in Darwin BSD syntax compared to Free BSD or Open BSD .

I am going to setup a Mac OS X info site because I love apple .
 

Matrix Agent

Masochist Mascot
I would also recommend UNIX for dummies, give a little wider view covering aspects of BSD, Linux, different shells, ect.
 

96.9

Registered
I already use unix , but the book looks great for unix newcomers .

The orielly website has some great stuff if you cruise around the site , OS X tips and such .

I'm just figuring out fink right now , I have it setup and now I am gonna try er out .

http://fink.sourceforge.net
 

Yatcher

Registered
And I want to add one more thing..

[localhost:~/documents/personal] tanakinc% open To\ download.rtf
[localhost:~/documents/personal] tanakinc%

use "open file.extension" command to open things..
 

96.9

Registered
The open darwin site is pretty good stuff .

http://www.opendarwin.org

I have checked out a few of the orielly OS X books and they are kinda lacking for people who want OS X internals but if your new to bsd/unix then I think they may be of help .

My advice is look into basic C programming , understanding C fundamentals makes learning unix filesystem much easier I am finding .
 
Top