Thursday, 29 January 2015

Basic Unix commands

1.1 Listing files and directories

ls (list):

    The ls command ( lowercase L and lowercase S ) lists the contents of your current working directory.
Files beginning with a dot (.) are known as hidden files and usually contain important program configuration information.
To list all files in your home directory including those whose names begin with a dot, type 
  $ ls -a

1.2 Making Directories

mkdir (make directory):

make a subdirectory called unixstuff in your current working directory type
$ mkdir unixStuff

This will create a new subdirectory in your current working directory.
To see the directory you have just  created

$ ls

1.3 Changing to a different directory

cd (change directory):

The command cd directory means change the current working directory to 'directory'
To change to the directory you have just made, type

$ cd unixStuff

1.4 The directories . and ..

Still in the unixstuff directory, type
$ ls -a

in the unixstuff directory (and in all other directories), there are two special directories called (.) and (..)

The current directory (.)

In UNIX, (.) means the current directory, so typing
$ cd .

means stay where you are (the unixstuff directory).

The parent directory (..)

(..) means the parent of the current directory, so typing

$ cd ..
will take you one directory up the hierarchy (back to your home directory). Try it now.
Note: typing cd with no argument always returns you to your home directory. This is very useful if you are lost in the file system.

1.5 Pathnames

pwd (print working directory):

Pathnames enable you to work out where you are in relation to the whole file-system. For example, to find out the absolute pathname of your home-directory, type cd to get back to your home-directory and then type
$ pwd

The full pathname will look something like this -
/Users/Jitendra

1.6 More about home directories and pathnames

Understanding pathnames

First type cd to get back to your home-directory, then type
    $ ls unixstuff
to list the conents of your unixstuff directory.
Now type
$ls backups
You will get a message like this -

ls: backups: No such file or directory

The reason is, backups is not in your current working directory. To use a command on a file (or directory) not in the current working directory (the directory you are currently in), you must either cd to the correct directory, or specify its full pathname. To list the contents of your backups directory, you must type

$ ls unixstuff/backups

~ (your home directory):

Home directories can also be referred to by the tilde ~ character. It can be used to specify paths starting at your home directory. So typing

$ ls ~/unixstuff

will list the contents of your unixstuff directory, no matter where you currently are in the file system.

$ ls ~

will work same as ls. it will list the content of current working directory.

$ls ~/..

will list the content of the parent directory of the current working directory.



Summary


                             
CommandMeaning
lslist files and directories
ls -alist all files and directories
mkdirmake a directory
cd directorychange to named directory
cdchange to home-directory
cd ~change to home-directory
cd ..change to parent directory
pwddisplay the path of the current directory




2.1 Copying Files



cp (copy):

What we are going to do now, is to take a file stored in an open access area of the file system, and use the cp command to copy it to your unixstuff directory.

First, cd to your unixstuff directory.
$ cd unixstuff

syntax:
$ cp [path of the file] .(dot)
 $ cp ~/Desktop/Git_intro.txt .

Note: Don't forget the dot . at the end. Remember, in UNIX, the dot means the current directory.

The above command means copy the file Git_intro.txt from Desktop to the current directory, keeping the name the same.

2.2 Moving files

To move a file from one place to another, use the mv command. This has the effect of moving rather than copying the file, so you end up with only one file rather than two.
It can also be used to rename a file, by moving the file to the same directory, but giving it a different name.

cd to current working working directory then type

$ mv Git_intro.txt MyGit.txt

2.3 Removing files and directories

To delete (remove) a file, use the rm command.

$ rm Git_intro.txt

You can use the rmdir command to remove a directory (make sure it is empty first).
$ rmdir backups

2.4 Displaying the contents of a file on the screen

clear (clear screen):

$ clear

This will clear all text and leave you with the % prompt at the top of the window.

cat (concatenate):

The command cat can be used to display the contents of a file on the screen. Type:
$ cat Git_intro.txt

As you can see, the file is longer than than the size of the window, so it scrolls past making it unreadable

less:

The command less writes the contents of a file onto the screen a page at a time. Type
$ less Git_intro.txt

Press the [space-bar] if you want to see another page, and type [q] if you want to quit reading. As you can see, less is used in preference to cat for long files.

head:

The head command writes the first ten lines of a file to the screen.

$ head Git_intro.txt

$ head -5 Git_intro.txt
 This will writes the first 5 lines of a file.

tail:

The tail command writes the last ten lines of a file to the screen.
$ tail Git_intro.txt


2.5 Searching the contents of a file

Simple searching using less:

Using less, you can search though a text file for a keyword (pattern). For example, to search through science.txt for the word 'science', type

$ less Science
 This will show the first page of the file.

then, still in less, type a forward slash [/] followed by the word to search
/science

As you can see, less finds and highlights the keyword. Type [n] to search for the next occurrence of the word.

grep (don't ask why it is called grep):

 It searches files for specified words or patterns. 

$ grep science Science.txt

As you can see, grep has printed out each line containg the word science.

The grep command is case sensitive; it distinguishes between Science and science.

To ignore upper/lower case distinctions, use the -i option, i.e. type

$ grep -i science Science.txt

To search for a phrase or pattern, you must enclose it in single quotes (the apostrophe symbol). For example to search for spinning top, type

$ grep -i 'spinning top' Science.txt

Some of the other options of grep are: 
-v display those lines that do NOT match
-n precede each matching line with the line number
-c print only the total count of matched lines 


wc (word count):

To do a word count on science.txt, type
$ wc -w science.txt

To find out how many lines the file has, type:
$ wc -l science.txt

Summary


CommandMeaning
cp file1 file2copy file1 and call it file2
mv file1 file2move or rename file1 to file2
rm fileremove a file
rmdir directoryremove a directory
cat filedisplay a file
less filedisplay a file a page at a time
head filedisplay the first few lines of a file
tail filedisplay the last few lines of a file
grep 'keyword' filesearch a file for keywords
wc filecount number of lines/words/characters in file




3.1 File system security (access rights)

$ ls -l

you will see something like this
-rwxr-xr-x@ 1 Jitendra  staff  3905926 Jan 29 11:49 pro-git-chacon.pdf

Each file (and directory) has associated access rights, which may be found by typing ls -l. Also, ls -lg gives additional information as to which group owns the file.

You will get the details of your directory.
the left most string is of size 9.
the left most bit in string indicates that whether it's a file or directory.
If it is a directory it will show d otherwise - 
the first three(from left) shows the permission for the owner of the file.
Second three bits shows the permission for the group.
and last three bit shows permission for all others.


Access rights on files.

  • r (or -), indicates read permission (or otherwise), that is, the presence or absence of permission to read and copy the file 
  • w (or -), indicates write permission (or otherwise), that is, the permission (or otherwise) to change a file 
  • x (or -), indicates execution permission (or otherwise), that is, the permission to execute a file, where appropriate 

Access rights on directories.

  • r allows users to list files in the directory; 
  • w means that users may delete files from the directory or move files into it; 
  • x means the right to access files in the directory. This implies that you may read files in the directory provided you have read permission on the individual files. 
So, in order to read a file, you must have execute permission on the directory containing that file, and hence on any directory containing that directory as a subdirectory, and so on, up the tree. 

3.2 Changing access rights

chmod (changing a file mode)

Only the owner of a file can use chmod to change the permissions of a file. The options of chmod are as follows

              
SymbolMeaning
u
user
g
group
o
other
a
all
r
read
w
write (and delete)
x
execute (and access directory) 
+
add permission
-
take away permission

 to remove read write and execute permissions on the file biglist for the group and others, type

      $ chmod go-rwx bilglist

This will leave the other permissions unaffected. 
To give read and write permissions on the file biglist to all,

$ chmod a+rw biglist


3.3 process and jobs

A process is an executing program identified by a unique PID (process identifier). To see information about your processes, with their associated PID and status, type

$ ps

Running background processes:

To background a process, type an & at the end of the command line. 

eg. $ sleep 10
This will wait 10 seconds before returning the command prompt %. Until the command prompt is returned, you can do nothing except wait.

To run sleep in the background, type
eg. $ sleep 10 &

this command will returns job number and pid  something like this:
[1] 6650      
The & runs the job in the background and returns the prompt straight away, allowing you do run other programs while waiting for that one to finish.

The user is be notified of a job number (numbered from 1) enclosed in square brackets, together with a PID and is notified when a background process is finished. Backgrounding is useful for jobs which will take a long time to complete.


Backgrounding a current foreground process:

eg. $ sleep 1000

You can suspend the process running in the foreground by typing ^Z, i.e.hold down the [Ctrl] key and type [z]. Then to put it in the background, type

$ bg

3.4 Killing a process

kill (terminate or signal a process):

It is sometimes necessary to kill a process (for example, when an executing program is in an infinite loop).

To kill a job running in the foreground, type ^C (control c).

$ sleep 100
^c

To kill a suspended or background process, 

$ kill jobnumber


ps (process status):

Alternatively, processes can be killed by finding their process numbers (PIDs) and using kill PID_number


If a process refuses to be killed, uses the -9 option, i.e. type
eg. $kill -9 20277




No comments:

Post a Comment