Friday, August 6, 2010

A UNIX survival guide for students

1. Basic UNIX commands
    > ls
        List the contents in current directory.   
    > ls -alF
        List the contents in long format in the current directory.
    > cd directoryname
        Change current directory to another directory.
    > mkdir directoryname
        Create a directory.
    > cp file1.txt file2.txt
        Make a copy of file1.txt.
    > mv /home/harpreet/file1.txt /tmp/file2.txt
        Move the file to different location or can be used for renaming a file.
    > rm filename.txt
        Delete a file.
    > rm -R directoryname
        Delete the contents of a directory.
    > head -20 filename.txt
        Get 20 lines from the top of the file.
    > tail -30 filename.txt
        Get 30 lines from the bottom of the file.
2. vi filename.txt
    Edit a file.
    a. Esc dd
        Deletes the current line
    b. Esc u
        Undo the last action
    c. Esc :0       (zero)
        Move cursor to first line of file
    d. Esc $
        Move to end of current line
    e. Esc Shift+g
        Move to end of file
    f. Esc yy
        Copy the current line
        Esc y3y
        Copy the next three lines
    g. Esc p
        Paste the copied line(s)
    h. Esc :35
        Moves the cursor to line number 35
    i. Esc /wordtofind
        Find the word within the file. Press 'n' to move to the next occurrence.
3. > grep 'wordtofind' filename.txt
        Finds the line containing a string from the file
       grep -i 'wordtofind' filename.txt
        Case-insensitive find.

4. > find /tmp/directoryname -name test.txt
        Find a file with name 'test.txt' in the given path.

5. > find . -type f -exec grep -i Row2 {} \; -print
        Find the word 'row2' from all the files in current path.
        Also, shows the line containing an occurrence of the word.
6. > scp .
        Copy a file located on another server to the current directory on the present server.
    > scp file.txt
        Copy a file from current server to another server into your home directory.
    > scp file.txt
        Copy a file from one machine to another.   
7. > tar cvzf new_tar_file.tar.gz file1.txt file2.txt
        Create a tar file new_tar_file.tar.gz containing file1.txt and file2.txt
    > tar xvzf abc.tar.gz
        Untar the contents of a file abc.tar.gz to the current directory
8. awk - Let us create a file 'test.txt' with these contents
       Row1-column1 Row1-column2 Row1-column3 Row1-column4
   Row2-column1 Row2-column2-HARPREET Row2-column3 Row2-column4
   Row3-column1 Row3-column2 Row3-column3 Row3-column4-SINGH
    Observe the output for various commands
    >  awk '{print $2}' test.txt
        The above command prints the second column
    String matching using awk :
    >  awk '{if ( $4 ~ /SINGH/ ) { print $2 $3}}' test.txt
        The above command matches the string 'SINGH' and prints column 2 and 3.   

Corrections/improvements/questions are welcome :)


Harpreet Singh said...

>find foldername* -name file.conf | xargs grep 'harpreet'

Find all files with name file.conf, in folders starting with foldername', which have the text 'harpreet'

Harpreet Singh said...

>find $FOLDER -name "*.log" -exec rm '{}' \;

Find and delete all files of extension type ".log" from a folder/directory.

Harpreet Singh said...

find all files with a pattern and containing a string-
> find . -type f -name "*.log" | xargs grep -l abcdef

Harpreet Singh said...

Replace 'apple' by 'nokia' in all files in the directory.
> grep -rl 'apple' ./ | xargs sed -i 's/apple/nokia/g'