Tuesday, February 16, 2010

Summary Unix Commands

Directories and Path:
/                              "root" directory
/usr                        directory usr (sub-directory of / "root" directory)

Moving around the file system:
pwd                       Show the current directory.
cd                            Change current directory to your HOME directory.
cd /usr/isura      Change current directory to /usr/isura.
cd INIT                  Change current directory to INIT which is a  sub-directory of the current directory.
cd ..                        Change current directory to the parent directory
cd $STRMWORK               Change current directory to the directory defined by the environment variable            
cd ~bob              Change the current directory to the user bob's home directory (if you have permission).

List directory contents:
ls                             list a directory
ls -l                         list a directory in long ( detailed ) format
ls -a                        List the current directory including hidden files.
ls -ld *                    List all the file and directory names in the current directory using long format.

Changing file permissions and attributes
chmod 755 file                   Changes the permissions of file.
chgrp user file                   Makes file belong to the group user.
chown cliff file                   Makes cliff the owner of file.
chown -R cliff dir              Makes cliff the owner of dir and everything in its directory tree.

Moving, renaming, and copying files:
cp file1 file2                        copy a file
mv file1 newname          move or rename a file
mv file1 ~/AAA/               move file1 into sub-directory AAA in your home directory.
rm file1                                 remove or delete a file
rm -r dir1                             recursively remove a directory and its contents
mkdir dir1                            create directories
mkdir -p dirpath               create the directory dirpath, including all implied directories in the path.
rmdir dir1                             remove an empty directory

Viewing and editing files:
cat filename                        Dump a file to the screen in ascii.
more filename                  Progressively dump a file to the screen:  ENTER = one line down
                                                SPACEBAR = page down               q=quit
vi filename                          Edit a file using the vi editor. 
emacs filename                Edit a file using the emacs editor. Not all systems will have emacs.

head filename                   Show the first few lines of a file.
head -n  filename            Show the first n lines of a file.
tail -n filename                  Show the last n lines of a file.

echo $SHELL                       You can find out what shell you are using by the command:
you can create a file with a list of shell commands and execute. This is called a shell script. This is in fact the primary purpose of most shells( bat files in Windows). 

Environment variables
You can teach your shell to remember things for later using environment variables.
For example under the bash shell:

export CASROOT=/usr/local/CAS3.0               Defines the variable CASROOT with the value
/usr/local/CAS3.0.           By prefixing $ to the variable name, you can evaluate it in any command:

cd $CASROOT         Changes your present working directory to the  value of CASROOT
echo $CASROOT       Prints out the value of CASROOT, or /usr/local/CAS3.0
printenv CASROOT    Does the same thing in bash and some other shells.

grep string filename > newfile           Redirects  the output of the above grep command to a file
grep string filename >> existfile        Appends the output of the grep command to the end of


ls -l | more         This commands takes the output of the long format directory list command
"ls -l" and pipes it through the more command (also known as a filter).

du -sc * | sort -n | tail 

The command "du -sc" lists the sizes of all files and directories in the current working directory. That is piped through "sort -n" which orders the output from smallest to largest size. Finally, that output is piped through "tail" which displays only the last few (which just happen to be the largest) results.

Command Substitution
You can use the output of one command as an input to another command in another way called command substitution. 
cat `find . -name aaa.txt`              which will cat ( dump to the screen ) all the files named aaa.txt that
exist in the current directory or in any subdirectory tree.

Searching for strings in files: The grep  command
grep string filename    prints all the lines in a file that contain the string

Searching for files : The find command

find search_path -name filename
find . -name aaa.txt    Finds all the files named aaa.txt in the current directory or any subdirectory tree.
find / -name vimrc      Find all the files named 'vimrc' anywhere on the system.
find /usr/local/games -name "*xpilot*"       Find all files whose names contain the string 'xpilot' which exist within the '/usr/local/games' directory tree.

Reading and writing tapes, backups, and archives: The tar

tar xv      Extracts (x) files from the default tape drive while listing (v = verbose)    the file names to the screen.

tar tv      Lists the files from the default tape device without extracting them.    
tar xvf archive.tar                       extract from the archive file
tar cvfz archive.tar.gz dname      Create a gzip compressed tar archive containing everything in the directory 'dname'. This does not work with all versions of tar.

tar xvfz archive.tar.gz               Extract a gzip compressed tar archive.  Does not work with all versions of tar.

compress part.igs    Creates a compressed file part.igs.Z
uncompress part.igs  Uncompresseis part.igs from the compressed file part.igs.Z.
gzip usually gives better compression than standard compress,

gzip part.igs     Creates a compressed file part.igs.gz
gunzip part.igs   Extracts the original file from part.igs.gz

Looking for help: The man and apropos
man ls      Shows the manual page for the ls command
apropos build     Shows a list of all the man pages whose discriptions contain the word "build"

Basics of the  vi editor
Opening a file      vi filename 
Creating text
i      Insert before current cursor position
I      Insert at beginning of current line
a      Insert (append) after current cursor position
A     Append to end of line
r      Replace 1 character
R     Replace mode

Terminate insertion or overwrite mode
Deletion of text
x      Delete single character
dd    Delete current line and put in buffer ndd   Delete n lines (n is a number) and put them in buffer
D     delete the remainder of the line, starting with current cursor position
J      Attaches the next line to the end of the current line (deletes carriage return).
u           Undo last command
yy         Yank current line into buffer
nyy       Yank n lines into buffer
p           Put the contents of the buffer after the current line
P           Put the contents of the buffer before the current line
cursor positioning
ctrl + d    Page down
ctrl + u    Page up
:n             Position cursor at line n
:$             Position cursor at end of file
ctrl + g    Display current line number
j               move cursor down one line
k               move cursor up one line
0(zero)    go to begging of the line
$               go to end of the line

:0(zero)   go to first line of the file
:$              go to last line of the file
string substitution
:n1,n2:s/string1/string2/[g]       Substitute string2 for string1 on lines n1 to n2. If g is included (meaning global),  all instances of string1 on each lineare substituted. If g is not included,only the first instance per matching line issubstituted.

:1,$:s/dog/cat/g               Substitute 'cat' for 'dog', every instance for the entire file - lines 1 to $ (end of file) Saving and quitting and other "ex" commands

:w                                          Write the current file.
:w new.file                          Write the file to the name 'new.file'.
:w! existing.file                   Overwrite an existing file with the file currently being edited.
:wq                                        Write the file and quit.
:q                                           Quit.
:q!                                          Quit with no changes.
:e filename                          Open the file 'filename' for editing.
:set number                         Turns on line numbering
:set nonumber                    Turns off line numbering

Searching and Determining line numbers
/string                        string search forward 
?string                       string search backward
n                               move next occurrence forward
N                              move next occurrence forward
:=                            Return the number of lines in the bottom of the screen

Read individual characters of a file(can be use to identify strange characters)
od -c filename

Compare 2 files and export difference 
cat file1.txt file2.txt | sort |uniq -u > out.txt

Convert Dos file to Unix
vi the document and type :set ff=unix

Show no of columns group by column length (delimiter 'tab')
$ awk -F '\t' ' {print NF+1 } ' unique_medical_0-55k.txt  | sort | uniq -c
Export no of columns in each line (delimiter 'tab')
$awk -F '\t' ' {print NF+1 } ' unique_medical_0-55k.txt  > /tmp/a
you can use /[^39]command to find out row no of column whose length except 39

No comments:

Post a Comment