Thursday 18 October 2012

Basics Of UNIX

The purpose of this post is to have a single page of frequently used basics commands for getting started with UNIX.

Basic UNIX Command Line (shell) navigation:

Directories:
Directories: 
Moving around the file system: 
Listing directory contents: 
Changing file permissions and attributes 
Moving, renaming, and copying files: 
Viewing and editing files:


Directories:

File and directory paths in UNIX use the forward slash "/" to separate directory names in a path.

examples:
/root directory
/usrdirectory usr (sub-directory of / "root" directory)
/usr/STRIM100STRIM100 is a sub directory of /usr



Moving around the file system:



pwd                            Show the "present working directory", or current directory.
cd                               Change current directory to your HOME directory.
cd /usr/STRIM100  Change current directory to /usr/STRIM100.
cd INIT                       Change current directory to INIT which is a sub-directory of
the current directory.
cd ..                             Change current directory to the parent directory of the current directory.
cd $STRMWORK      Change current directory to the directory defined by the environment variable 'STRMWORK'.
cd ~bob                      Change the current directory to the user bob's home directory
(if you have permission).



Listing directory contents:

lslist a directory
ls -llist a directory in long ( detailed ) format



for example:
$ ls -l
drwxr-xr-x    4 cliff    user        1024 Jun 18 09:40 WAITRON_EARNINGS
-rw-r--r--    1 cliff    user      767392 Jun  6 14:28 scanlib.tar.gz
^ ^  ^  ^     ^   ^       ^           ^      ^    ^      ^
| |  |  |     |   |       |           |      |    |      | 
| |  |  |     | owner   group       size   date  time    name
| |  |  |     number of links to file or directory contents
| |  |  permissions for world
| |  permissions for members of group
| permissions for owner of file: r = read, w = write, x = execute -=no permission
type of file: - = normal file, d=directory, l = symbolic link, and others...

ls -a        List the current directory including hidden files. Hidden files start
             with "."
ls -ld *     List all the file and directory names in the current directory using
             long format. Without the "d" option, ls would list the contents
             of any sub-directory of the current. With the "d" option, ls
             just lists them like regular files.

Changing file permissions and attributes:

chmod 755 filenameChanges the permissions of file to be rwx for the owner, and rx
for the group and the world. (7 = rwx = 111 binary. 5 = r-x = 101 binary)
chgrp user filenameMakes file belong to the group user.
chown cliff filenameMakes cliff the owner of file.
chown -R cliff dirMakes cliff the owner of dir and everything in its directory tree.


You must be the owner of the file/directory or be root before you can do any of these things.


Moving, renaming, and copying files:


cp file1 file2copy a file
mv file1 newnamemove or rename a file
mv file1 ~/AAA/move file1 into sub-directory AAA in your home directory.
rm file1 [file2 ...] remove or delete a file
rm -r dir1 [dir2...]recursivly remove a directory and its contents BE CAREFUL!
mkdir dir1 [dir2...]create directories
mkdir -p dirpathcreate the directory dirpath, including all implied directories in the path.
rmdir dir1 [dir2...]remove an empty directory


Viewing and editing files:

cat filenameDump a file to the screen in ascii.
more filenameProgressively dump a file to the screen: ENTER = one line down
SPACEBAR = page down  q=quit
less filenameLike more, but you can use Page-Up too. Not on all systems.
vi filenameEdit a file using the vi editor. All UNIX systems will have vi in some form.
emacs filenameEdit a file using the emacs editor. Not all systems will have emacs.
head filenameShow the first few lines of a file.
head -n  filenameShow the first n lines of a file.
tail filenameShow the last few lines of a file.
tail -n filenameShow the last n lines of a file.


Back to top

No comments:

Post a Comment