Monday, 29 July 2013

Basic Unix Commands




  • cat --- for creating and displaying short files
  • chmod --- change permissions
  • cd --- change directory
  • cp --- for copying files
  • date --- display date
  • echo --- echo argument
  • ftp --- connect to a remote machine to download or upload files
  • grep --- search file
  • head --- display first part of file
  • ls --- see what files you have
  • lpr --- standard print command (see also print )
  • more --- use to read files
  • mkdir --- create directory
  • mv --- for moving and renaming files
  • ncftp --- especially good for downloading files via anonymous ftp.
  • print --- custom print command (see also lpr )
  • pwd --- find out what directory you are in
  • rm --- remove a file
  • rmdir --- remove directory
  • rsh --- remote shell
  • setenv --- set an environment variable
  • sort --- sort file
  • tail --- display last part of file
  • tar --- create an archive, add or extract files
  • telnet --- log in to another machine
  • wc --- count characters, words, lines

cat
This is one of the most flexible Unix commands. We can use to create, view and concatenate files. For our first example we create a three-item English-Spanish dictionary in a file called "dict."
   % cat >dict
     red rojo
     green verde
     blue azul
<control-D>
   %
<control-D> stands for "hold the control key down, then tap 'd'". The symbol > tells the computer that what is typed is to be put into the file dict. To view a file we use cat in a different way:
   % cat dict
     red rojo
     green verde
     blue azul
   %
If we wish to add text to an existing file we do this:
   % cat >>dict
     white blanco
     black negro
     <control-D>
   %
Now suppose that we have another file tmp that looks like this:
   % cat tmp
     cat gato
     dog perro
   %
Then we can join dict and tmp like this:
   % cat dict tmp >dict2
We could check the number of lines in the new file like this:
   % wc -l dict2
8
The command wc counts things --- the number of characters, words, and line in a file.

This command is used to change the permissions of a file or directory. For example to make a file essay.001 readable by everyone, we do this:
   % chmod a+r essay.001
To make a file, e.g., a shell script mycommand executable, we do this
   % chmod +x mycommand
Now we can run mycommand as a command.
To check the permissions of a file, use ls -l . For more information on chmod, use man chmod.

Use cd to change directory. Use pwd to see what directory you are in.
   % cd english
   % pwd
   % /u/ma/jeremy/english
   % ls
novel poems
   % cd novel
   % pwd
   % /u/ma/jeremy/english/novel
   % ls
ch1 ch2 ch3 journal scrapbook
   % cd ..
   % pwd
   % /u/ma/jeremy/english
   % cd poems
   % cd
   % /u/ma/jeremy
Jeremy began in his home directory, then went to his english subdirectory. He listed this directory using ls , found that it contained two entries, both of which happen to be diretories. He cd'd to the diretory novel, and found that he had gotten only as far as chapter 3 in his writing. Then he used cd .. to jump back one level. If had wanted to jump back one level, then go to poems he could have said cd ../poems. Finally he used cd with no argument to jump back to his home directory.

Use cp to copy files or directories.
   % cp foo foo.2
This makes a copy of the file foo.
   % cp ~/poems/jabber .
This copies the file jabber in the directory poems to the current directory. The symbol "." stands for the current directory. The symbol "~" stands for the home directory.

Use this command to check the date and time.
   % date
Fri Jan  6 08:52:42 MST 1995

The echo command echoes its arguments. Here are some examples:
   % echo this
     this
   % echo $EDITOR
     /usr/local/bin/emacs
   % echo $PRINTER
     b129lab1
Things like PRINTER are so-called environment variables. This one stores the name of the default printer --- the one that print jobs will go to unless you take some action to change things. The dollar sign before an environment variable is needed to get the value in the variable. Try the following to verify this:
   % echo PRINTER
     PRINTER

Use ftp to connect to a remote machine, then upload or download files. See also: ncftp
Example 1: We'll connect to the machine fubar.net, then change director to mystuff, then download the file homework11:
   % ftp solitude
     Connected to fubar.net.
     220 fubar.net FTP server (Version wu-2.4(11) Mon Apr 18 17:26:33 MDT 1994) ready.
   Name (solitude:carlson): jeremy
     331 Password required for jeremy.
   Password:
     230 User jeremy logged in.
   ftp> cd mystuff
     250 CWD command successful.
   ftp> get homework11
   ftp> quit
Example 2: We'll connect to the machine fubar.net, then change director to mystuff, then upload the file collected-letters:
   % ftp solitude
     Connected to fubar.net.
     220 fubar.net FTP server (Version wu-2.4(11) Mon Apr 18 17:26:33 MDT 1994) ready.
   Name (solitude:carlson): jeremy
     331 Password required for jeremy.
   Password:
     230 User jeremy logged in.
   ftp> cd mystuff
     250 CWD command successful.
   ftp> put collected-letters
   ftp> quit
The ftp program sends files in ascii (text) format unless you specify binary mode:
   ftp> binary
   ftp> put foo
   ftp> ascii
   ftp> get bar
The file foo was transferred in binary mode, the file bar was transferred in ascii mode.

Use this command to search for information in a file or files. For example, suppose that we have a file dict whose contents are
   red rojo
   green verde
   blue azul
   white blanco
   black negro
Then we can look up items in our file like this;
   % grep red dict
     red rojo
   % grep blanco dict
     white blanco
   % grep brown dict
   %
Notice that no output was returned by grep brown. This is because "brown" is not in our dictionary file.
Grep can also be combined with other commands. For example, if one had a file of phone numbers named "ph", one entry per line, then the following command would give an alphabetical list of all persons whose name contains the string "Fred".
   % grep Fred ph | sort
     Alpha, Fred: 333-6565
     Beta, Freddie: 656-0099
     Frederickson, Molly: 444-0981
     Gamma, Fred-George: 111-7676
     Zeta, Frederick: 431-0987
The symbol "|" is called "pipe." It pipes the output of the grep command into the input of the sort command.
For more information on grep, consult
   % man grep

Use this command to look at the head of a file. For example,
   % head essay.001
displays the first 10 lines of the file essay.001 To see a specific number of lines, do this:
   % head -n 20 essay.001
This displays the first 20 lines of the file.

Use ls to see what files you have. Your files are kept in something called a directory.
   % ls
     foo       letter2
     foobar    letter3
     letter1   maple-assignment1
   %
Note that you have six files. There are some useful variants of the ls command:
   % ls l*
     letter1 letter2 letter3
   %
Note what happened: all the files whose name begins with "l" are listed. The asterisk (*) is the " wildcard" character. It matches any string.

This is the standard Unix command for printing a file. It stands for the ancient "line printer." See
   % man lpr
for information on how it works. See print for information on our local intelligent print command.

Use this command to create a directory.
   % mkdir essays
To get "into" this directory, do
   % cd essays
To see what files are in essays, do this:
   % ls
There shouldn't be any files there yet, since you just made it. To create files, see cat or emacs.

More is a command used to read text files. For example, we could do this:
   % more poems
The effect of this to let you read the file "poems ". It probably will not fit in one screen, so you need to know how to "turn pages". Here are the basic commands:
  • q --- quit more
  • spacebar --- read next page
  • return key --- read next line
  • b --- go back one page
For still more information, use the command man more.

Use this command to change the name of file and directories.
   % mv foo foobar
The file that was named foo is now named foobar

Use ncftp for anonymous ftp --- that means you don't have to have a password.
   % ncftp ftp.fubar.net
     Connected to ftp.fubar.net
   > get jokes.txt
The file jokes.txt is downloaded from the machine ftp.fubar.net.

This is a moderately intelligent print command.
   % print foo
   % print notes.ps
   % print manuscript.dvi
In each case print does the right thing, regardless of whether the file is a text file (like foo ), a postcript file (like notes.ps, or a dvi file (like manuscript.dvi. In these examples the file is printed on the default printer. To see what this is, do
   % print
and read the message displayed. To print on a specific printer, do this:
   % print foo jwb321
   % print notes.ps jwb321
   % print manuscript.dvi jwb321
To change the default printer, do this:
   % setenv PRINTER jwb321

Use this command to find out what directory you are working in.
   % pwd
/u/ma/jeremy
   % cd homework
   % pwd
/u/ma/jeremy/homework
   % ls
assign-1 assign-2 assign-3
   % cd
   % pwd
/u/ma/jeremy
   %
Jeremy began by working in his "home" directory. Then he cd 'd into his homework subdirectory. Cd means " change directory". He used pwd to check to make sure he was in the right place, then used ls to see if all his homework files were there. (They were). Then he cd'd back to his home directory.

Use rm to remove files from your directory.
   % rm foo
     remove foo? y
   % rm letter*
     remove letter1? y
     remove letter2? y
     remove letter3? n
   %
The first command removed a single file. The second command was intended to remove all files beginning with the string "letter." However, our user (Jeremy?) decided not to remove letter3.

Use this command to remove a directory. For example, to remove a directory called "essays", do this:
   % rmdir essays
A directory must be empty before it can be removed. To empty a directory, use rm.

Use this command if you want to work on a computer different from the one you are currently working on. One reason to do this is that the remote machine might be faster. For example, the command
   % rsh solitude
connects you to the machine solitude. This is one of our public workstations and is fairly fast.
See also: telnet

   % echo $PRINTER
     labprinter
   % setenv PRINTER myprinter
   % echo $PRINTER
     myprinter

Use this commmand to sort a file. For example, suppose we have a file dict with contents
red rojo
green verde
blue azul
white blanco
black negro
Then we can do this:
   % sort dict
     black negro
     blue azul
     green verde
     red rojo
     white blanco
Here the output of sort went to the screen. To store the output in file we do this:
   % sort dict >dict.sorted
You can check the contents of the file dict.sorted using cat , more , or emacs .

Use this command to look at the tail of a file. For example,
   % tail essay.001
displays the last 10 lines of the file essay.001 To see a specific number of lines, do this:
   % tail -n 20 essay.001
This displays the last 20 lines of the file.

Use create compressed archives of directories and files, and also to extract directories and files from an archive. Example:
   % tar -tvzf foo.tar.gz
displays the file names in the compressed archive foo.tar.gz while
   % tar -xvzf foo.tar.gz
extracts the files.

Use this command to log in to another machine from the machine you are currently working on. For example, to log in to the machine "solitude", do this:
   % telnet solitude
See also: rsh.

Use this command to count the number of characters, words, and lines in a file. Suppose, for example, that we have a file dict with contents
red rojo
green verde
blue azul
white blanco
black negro
Then we can do this
   % wc dict
     5      10      56 tmp
This shows that dict has 5 lines, 10 words, and 56 characters.
The word count command has several options, as illustrated below:
   % wc -l dict
     5 tmp
   % wc -w dict
     10 tmp
   % wc -c dict
     56 tmp    

Under construction


What Do Permissions Do?

Permissions determine which users can read, write or execute the files that you own. In the case of directories, they determine which users can access, delete or list the contents of  directories that you own.
Commercial Unix systems (including ours) allow you to specify permissions down to the individual user (in order to meet US government C2 security) , but we don't use that here. If you think chmod 644 is complex.... :)
I'm assuming that Unix  knowledge is limited, so there'll be some simple introductions to commands, parameters and pipes along the way...I'm also going to leave some things out...

Viewing Permissions

ls -l filename or directoryname
$ls -l test.out
-rw-r--r-- 1 spaniel dogs 1435 Nov 24 10:17 test.out
If you type ls -l with no filename, it lists the files and directories in your current directory.
$ls -l
total 4085
-rw----r-- 1 spaniel dogs 2360 Aug 26 1997 assign.htm
-rw-rw-r-- 1 terrier dogs 97230 Feb 13 1996 backup.pdf
-rw-r--r-- 1 spaniel dogs 6703 Feb 20 13:10 backup_contrib_users.htm
-rw-r--r-- 1 spaniel dogs 6185 Sep 23 1997 backup_tips.htm
-rw----r-- 1 spaniel dogs 2110 May 30 1997 banshee.htm
-rw----r-- 1 spaniel dogs 1882 Oct 25 1996 bansheemac.htm
-rw-rw-r-- 1 spaniel dogs 56109 Feb 08 1996 care.pdf
drwxr-xr-x 2 spaniel dogs 1024 Nov 03 16:21 efficientlynt
drwxr-xr-x 2 spaniel dogs 1024 Mar 19 1997 email_class
If you type ls -l directory name, it lists the files inside the directory
$ls -l docs
total 4096
-rw----r-- 1 spaniel dogs 2360 Aug 26 1997 assign.htm
-rw-rw-r-- 1 spaniel dogs 97230 Feb 13 1996 backup.pdf
To display the permissions on the directory only, use ls -ld
$ls -ld docs
drwxrwxr-x 29 spaniel dogs 2048 Jun 11 11:02 docs
I like ls -lF because F shows directories with a / at the end (it also shows symbolic links with @ and executables with  *).
$ls -lF
total 2
-rw-r--r-- 2 spaniel dogs 0 Apr 23 14:54 1.hard_link
lrwxrwxrwx 1 spaniel dogs 1 Apr 23 15:04 1.symbolic_link@ -> 1
-rw-r--r-- 2 spaniel dogs 0 Apr 23 14:54 1.txt
-rw-r--r-- 1 spaniel dogs 0 Apr 23 14:55 2.txt
-rw-r--r-- 1 spaniel dogs 0 Apr 23 14:55 3.txt
-rw-r--r-- 1 spaniel dogs 0 Apr 23 14:55 4.txt
-rw-r--r-- 1 spaniel dogs 0 Apr 23 14:55 5.txt
drwxr-xr-x 2 spaniel dogs 512 Apr 23 14:55 dir1/
drwxr-xr-x 2 spaniel dogs 512 Apr 23 14:55 dir2/
-rwxr-xr-x 1 spaniel dogs 0 Apr 23 15:10 executable_script*
If you have more than one screen of files, you can use a pipe to send the output of ls to more, which is a pager. Hit the space bar to advance one screen or the enter key to advance one line in more.
Sidebar: What's a pipe? It's a way for 2 processes (ls and more) to communicate. In the unix shell, a pipe is symbolized with the | symbol. It says, take the standard output of ls, which usually goes to your screen, and, instead of sending it to the screen, send it to the standard input of another command, more.
$ls -l | more
total 4086
-rw----r-- 1 spaniel dogs 2360 Aug 26 1997 assign.htm
-rw-rw-r-- 1 terrier dogs 97230 Feb 13 1996 backup.pdf
-rw-r--r-- 1 spaniel dogs 6703 Feb 20 13:10 backup_contrib_users.htm
-rw-r--r-- 1 spaniel dogs 6185 Sep 23 1997 backup_tips.htm
-rw----r-- 1 spaniel dogs 2110 May 30 1997 banshee.htm
-rw----r-- 1 spaniel dogs 1882 Oct 25 1996 bansheemac.htm
-rw-rw-r-- 1 spaniel dogs 56109 Feb 08 1996 care.pdf
drwxr-xr-x 2 spaniel dogs 1024 Nov 03 16:21 efficientlynt
drwxr-xr-x 2 spaniel dogs 1024 Mar 19 1997 email_class
drwxrwxr-x 2 spaniel dogs 512 Nov 03 16:32 emergency_repair
-rw-r--r-- 1 terrier dogs 485763 Apr 17 1996 eudora.pdf
-rw-r--r-- 1 spaniel dogs 10066 May 16 1996 eudora3.pdf
drwxr-xr-x 2 spaniel dogs 512 Jul 02 1997 eudora_configuration_change
-rw----r-- 1 spaniel dogs 3086 Feb 23 13:44 eudora_transfer.htm
-rw----r-- 1 spaniel dogs 1508 Jun 09 1997 eudoramac.htm
drwxrwxr-x 2 spaniel dogs 512 Apr 03 09:16 expert_user_handbook
drwxrwx--- 2 shepard dogs 512 Apr 03 09:16 expert_user_only
-rw-rw-r-- 1 terrier dogs 224686 Feb 09 1996 filemgr.pdf
-rw-rw-r-- 1 spaniel dogs 130095 Feb 08 1996 fileshar.pdf
drwxr-xr-x 2 spaniel dogs 512 Jun 05 1997 formmanual
-rw-r--r-- 1 spaniel dogs 888 Aug 26 1997 forms.htm
lrwxrwxrwx 1 spaniel dogs 25 Sep 28 1997 graphictiny.gif -> /graphic
Reading the output of ls -l to determine the permissions of a file:
-rw-rw-r-- 1 spaniel dogs 56109 Feb 08 1996 care.txt
drwxr-xr-x 2 spaniel dogs 1024 Nov 03 16:21 efficientlynt
-
rw-rw-r--
1
spaniel
dogs
56109
Feb 08 1996
care.txt
d
rwxr-xr-x
2
spaniel
dogs
1024
Nov 03 6:21
efficientlynt
File Type: 
- regular file 
d directory 
l symbolic link 
other characters are usually I/O devices
Permissions
# of 
links
Owner
Group 
owner
Size in bytes
Date
Name
Every file and directory has nine permissions associated with it.
Files and directories have three types of permissions:
  • r (read)
  • w (write)
  • x (execute)
These three permissions occur for each of the following classes of users:
  • u (user/owner)
  • g (group)
  • o (other or world)
What this means:
  • r allows you to view or print a file
  • w allows you to write (modify) a file
  • x allows you to execute a file or search a directory
Permissions
As applied to FILES
As applied to DIRECTORIES
r (read)
Can be viewed or printed
Contents can be read, but not searched. Normally r and x are used together
w (write)
Contents can be changed or deleted
Entries can be added or removed
x (execute)
File can be used as a program
Directory can be searched
If you own a file, you can change permissions with the chmod command.
If you own a file, you can change the group with the chgrp command.

Default Permissions

  • When you ftp a file the following permissions are set automatically:
    -rw-r----- 1 spaniel dogs 1508 May 27 10:10 yyy
  • If you create a file directly on the server, the default permissions are different:
    -rw-r--r-- 1 spaniel dogs 0 May 27 10:14 zzz
  • If you create a directory with ftp:
    drwxr-x--- 2 spaniel dogs 512 May 27 10:16 dir1
  • If you create a directory directly on the server with mkdir:
    drwxr-xr-x 2 spaniel dogs 512 May 27 10:17 dir2
Default permissions are controlled by your umask and site umask (for ftp). You can change them, but that's beyond the scope of this class. You can also ignore the whole issue, since you probably will set the permissions manually anyway.

Changing permissions

Octal Method: You can change permissions with the chmod command using letters and symbols (symbolic method) or using numbers 0-7(octal method). Changing permissions can be very confusing, and there are multiple ways to accomplish what you want, so I'm not going to cover everything.
Most people here use the "octal" method of setting permissions:
$chmod 644 file4
$ls -l file4
-rw-r--r-- 1 spaniel dogs 0 May 26 12:01 file4
0
No permissions
1
Execute
2
Write
4
Read
The permissions are cumulative: I add 4+2 (read +write) and get 6. Any combination is legal, but not necessarily meaningful :)
If I want members of the group dogs to write to this file, I would set it to 664:
-rw-rw-r-- 1 spaniel dogs 0 May 26 12:01 file4
Remember that a user coming in with a web browser is subject to the permissions set for other.
Directories are a little different:
755 is a common directory setting:
drwxr-xr-x 2 spaniel dogs 512 May 26 13:46 dir1
The owner has read write and execute and group and other have read and execute. Remember, other needs read and execute on a directory in order for a web browser to view files in that directory. Although you can give read but not execute (or vice-versa) on a directory, there's almost no reason for it.
You'd have to set it 775 if you wanted members of the group to be able to write to it:
drwxrwxr-x 2 spaniel dogs 512 May 26 13:46 dir1
Keep in mind that if you give someone write permission to a directory, they can delete any file in the directory, even if they don't have permission to write or read that specific file.
Letters and Symbols: You can also change permissions using a combination of letters and symbols. Some people think this is easier. Use whichever you prefer. I tend to use both, depending on the situation.
The general format of the command is chmod userclass operation permission
Userclass
User (owner)
g
Group
o
Other
a
All (user, group, other)
Operation
+
Add permission
-
Remove permission
Assign permission regardless of previous setting
Permission
r
Read
s
Set user or group ID (Don't worry about this for now)
w
Write
x
Execute
$ls -l trak
-r--r--r-- 1 spaniel dogs 1207 Apr 01 10:10 trak
User, group and other are all set for read only. If I want to give user and group write access, I would type:
$chmod ug+w trak
$ls -l trak
-rw-rw-r-- 1 spaniel dogs 1207 Apr 01 10:10 trak
Userclass is user and group, operation is add, and permission is write
Notice that when you use add or remove, it only affects permissions directly addressed by the command. The file already had read access for everyone and my command didn't touch that, since it was addressing write access only. This can be a helpful feature of this method of setting permissions.
If you want to make multiple changes, separate them with commas
$chmod ug+w,o+r trak
Using the = operator resets ALL permissions on the file, regardless of how the were set previously.
$ls -l trak
-rw-rw-r-- 1 spaniel dogs 1207 Apr 01 10:10 trak
$chmod a=r trak
$ls -l trak
-r--r--r-- 1 spaniel dogs 1207 Apr 01 10:10 trak
Notice that even though the command only addressed read permissions, it also cleared the write permissions.

Groups

Every user on a Unix system is a member of at least one group. Most are probably members of several groups. Groups are a convenient way to let muliple users access file based on shared needs, without letting everyone access them. Everyone has a "primary group." If you are only in one group, that's your primary group. To find out what groups you or someone else are in, type id username at the shell prompt.
$id mmouse
uid=220(mmouse)gid=208(rodents)groups=1(staff),216(cats)
Your primary group is listed first (rodents) and all other groups are listed after.
When you create a file on the server, or ftp one there, you are the owner of the file, and the group is your primary group. This may cause a complication for mmouse if mmouse is working with members of the group cats when her primary or default group is rodents
-rw-rw-r-- 1 mmouse rodents 1207 Apr 01 10:10 trak
The permissions are set to allow the group to write, but it's the wrong group!
You can change the group with the chgrp command. You must own the file and be a member of the group to which you are changing.
$chgrp cats trak
$ls -l trak
-rw-rw-r-- 1 mmouse cats 1207 Apr 01 10:10 trak

No comments:

Post a Comment