Showing posts with label General Unix Skills. Show all posts
Showing posts with label General Unix Skills. Show all posts

Thursday, July 7, 2016

Find Files Based on Access / Modification / Change Time

The basic syntax of the find command is:

find path options

where path is the path to search and options are the options given to find command.
You can find files based on following three file time attribute.

    • -amin  when the file was accessed in minutes
    • -atime when the file was accessed in days
    • -cmin when the file was created in minutes
    • -ctime when the file was created in days
    • -mmin when the file was modified in minutes
In all our below examples, the path is our current directory and hence we use .(dot).

1. To find files modified in the last 5 days:
find . -mtime -5
 
2. To find files modified before 5 days:
find . -mtime +5

 Note: Developers, be aware. + is not default in find. If you omit the '+', it has a different meaning. It means to find files modified exactly before 5 days.
3. To find files modified in the last 40mins:
find . -mmin -40 


4. To find files modified before 40mins:
find . -mmin +40

 
The above commands will find both files and directories modifying the criteria. 
 
If you want to find only files, use the -type option.
find . -type f -mmin -40
 
This will find only the files modified in the last 40 mins, not directories.
 
5. Find files whose content got updated within last 1 hour


# find . -mmin -60
In the same way, following example finds all the files (under root file system /) that got updated within the last 24 hours (1 day).

# find / -mtime -1
Example 2: Find files which got accessed before 1 hour
# find -amin -60
 
In the same way, following example finds all the files (under root file system /) that got accessed within the last 24 hours (1 day).


# find / -atime -1
Example 3: Find files which got changed exactly before 1 hour
# find . -cmin -60
 
In the same way, following example finds all the files (under root file system /) that got changed within the last 24 hours (1 day).


# find / -ctime -1
Example 9: ls -l in find command output. Long list the files which are edited within the last 1 hour.
# find -mmin -60


Sunday, December 20, 2015

RHEL 7: Install and setup samba server


RHEL 7 : Install and setup samba server ( file sharing)

Operating System : RHEL7/CentOS 7
Arch : x86_64
Samba Version : Samba 4.1.1 (Default from CentOS repo)
Kernel Versio : 3.10.0-123.el7.x86_64
IP Address : 192.168.56.102
Network Subnet : 192.168.56.0/24 or can be written also as 192.168.56.0/255.255.255.0
NOTE: CentOS 7 is installed with minimal packages (minimal installed)

Samba Server port numbers

137/tcp
137/udp
138/tcp
138/udp
139/udp
139/udp
445/tcp
445/udp

Daemon used in Samba server

Samba services are implemented as two daemons:
1. smbd, It provides the file and printer sharing services.
2. nmbd It provides the NetBIOS-to-IP-address name service. NetBIOS over TCP/IP requires some method for mapping NetBIOS computer names to the IP addresses of a TCP/IP network.

How to install and configure samba server

Step 1 : Use yum command to install samba packages
(a) policycoreutils-python = For semanage command
(b) samba-client : For smbpasswd
(c) cups-libs : For printer service
(d) samba and samba-commons : For Samba server
 
yum install -y samba samba-commons cups-libs policycoreutils-python samba-client
 
Step 2: create a directory
Create a directory called sharedrepo in / (main root) . This directory will be shared with clients.
 
mkdir /home/smbshare

Step 3: Add a new group or can use existing group
To provide access on shared directory,Here we are adding new group called staff.
 
groupadd smb

Step 4: Change the group and permission of sharing folder
Here we are using /sharedrepo in samba server, hence group and permission are changing for this directory.
 
chgrp -R smb /sharedrepo
chmod -R 777 /sharedrepo

Step 5: Change the selinux security context
Change the selinux security context on sharing directory and set the selinux boolean value for samba.
You can skip this step in case you disable selinux on system.
 
# semanage fcontext -a -t samba_share_t /home/smbshare/
# restorecon -Rv /home/smbshare/
# setsebool -P samba_enable_home_dirs on

Step 6: create user, add into group and set samba password
create user and add them in group called staff. And set the samba password for this user.
 
# useradd smbuser
# usermod -aG smb smbuser
# smbpasswd -a smbuser
New SMB password:
Retype new SMB password:
Added user smbuser.

Step 7: Edit /etc/samba/smb.conf file
First take backup of /etc/samba/smb.conf file then edit the smb.conf file.
 
cd /etc/samba/
cp -p smb.conf smb.conf.orig

And add the below given contents in last line of /etc/samba/smb.conf file.
 
vi /etc/samba/smb.conf
[smbshare]
comment = shared-directory
path = /home/smbshare
public = no
valid users = sbmuser, @smb
writable = yes
browseable = yes
create mask = 0765


Step 8: Now start the smb and nmb services.
 
systemctl start smb.service
systemctl start nmb.service

Step 9 : Enable smb and nmb service at booting of system
 
systemctl enable smb.service
systemctl enable nmb.service


Step 10 : Add firewalld rule to allow samba

# firewall-cmd --add-service=samba --permanent

Now reload firewalld
# firewall-cmd --reload 

Note: firewalld service must be runnig for above procedure. To start firewalld use systemd command i.e systemctl start firewalld.service

How to connect to Samba Server

1. Windows :
In Windows Operatig System, open the run by pressing in combination of Start key + r. Then type in this format \\ip-address-of-samba-server\shared-

Direcory-name
 
\\192.168.56.102\sharedrepo

Give username and password when it will ask.

2. Linux :
smbclient must be installed on system .

(A) List the shared files or directory available in samba server
 
smbclient -L \\192.168.56.102 -U smbuser 

In above command,
-L = For listing shared objects.
Samba Server IP Address = 192.168.56.102
User Name =smbuser

Below given is sample output
 
sharad@linuxworld:~$ smbclient -L \\192.168.56.102 -U smbuser
Enter test's password: 
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]

 Sharename       Type      Comment
 ---------       ----      -------
 IPC$            IPC       IPC Service (Samba Server Version 4.1.1)
 sharedrepo      Disk      shared-directory
 test            Disk      Home Directories
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]

 Server               Comment
 ---------            -------
 LOCALHOST            Samba Server Version 4.1.1

 Workgroup            Master
 ---------            -------
 MYGROUP              LOCALHOST
sharad@linuxworld:~$

(B) Access using smb console
 
smbclient //192.168.56.102/sharedrepo -U smbuser

After login, you will get smb console. You can use get and put command for getting/putting the file. There are other commands you can use also.
Sample output
 
sharad@linuxworld:~$ smbclient //192.168.56.102/sharedrepo -U smbuser
Enter test's password: 
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> 
smb: \> help
?              allinfo        altname        archive        backup         
blocksize      cancel         case_sensitive cd             chmod          
chown          close          del            dir            du             
echo           exit           get            getfacl        geteas         
hardlink       help           history        iosize         lcd            
link           lock           lowercase      ls             l              
mask           md             mget           mkdir          more           
mput           newer          notify         open           posix          
posix_encrypt  posix_open     posix_mkdir    posix_rmdir    posix_unlink   
print          prompt         put            pwd            q              
queue          quit           readlink       rd             recurse        
reget          rename         reput          rm             rmdir          
showacls       setea          setmode        stat           symlink        
tar            tarmode        timeout        translate      unlock         
volume         vuid           wdel           logon          listconnect    
showconnect    tcon           tdis           tid            logoff         
..             !              
smb: \> 

(C) Mount the samba shared directory
Mount the samba shared directory in machine , your system must support cifs file system
mount -t cifs //192.168.56.102/sharedrepo -o username=test /mnt/

In Ubuntu, Click on dash home and simply access the samba server
 
smb://192.168.56.102/

See the sample screenshot of dash home.
dash home ubuntu

Tuesday, November 3, 2015

How to find larger file under Linux

List All Large Files

To finds all files over 1G in size and display their names, along with size, use following syntax:

Syntax for RedHat / CentOS / Fedora Linux

find {/path/to/directory/} -type f -size +{size-in-kb}k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }' 

        # find / -type f -size +1G -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'



Search or find big files Linux (50MB) in current directory, enter:
            $ find . -type f -size +50000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'

Syntax for Debian / Ubuntu Linux

find {/path/to/directory} -type f -size +{file-size-in-kb}k -exec ls -lh {} \; | awk '{ print $8 ": " $5 }'
Search in current directory:
           $ find . -type f -size +1G -exec ls -lh {} \; | awk '{ print $8 ": " $5 }'

Sample output:
./.kde/share/apps/akregator/Archive/http___blogs.msdn.com_MainFeed.aspx?Type=AllBlogs.mk4: 91M
./out/out.tar.gz: 828M
./.cache/tracker/file-meta.db: 101M
./ubuntu-8.04-desktop-i386.iso: 700M
./vivek/out/mp3/Eric: 230M

Friday, October 23, 2015

Configure NTP server for time adjustment.

Installing NTP Server

NTP packages are available under default repositories. You just login to your server as root user and execute following command.
# yum install ntp

Configure NTP Server

Sync Local Time from pool.ntp.org

First we need to keep synchronize time of this server. For this we will use NTP POOL cluster servers to synchronize time of local system. NTP POOL project is providing large number of cluster servers. Lets edit NTP configuration file and update following values
 
# vim /etc/ntp.conf
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
 
As NTP pool providing large number of server spread world-wide. We suggest to use any near by servers also. You can find nearby servers at http://www.pool.ntp.org/en/.

Allow LAN Systems

Now configure your ntp server to allow LAN systems to keep synchronize their time from this server. To do it add following entry in configuratio nfile
 restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
ntp-pool-settings
Add caption

Restart NTP Server

NTP server listen on UDP port 123. After making all above configuration, Let’s restart NTP server using following commands.

For CentOS/RHEL 7 and Fedora 21

# systemctl start ntpd
# systemctl enable ntpd

For CentOS/RHEL 6/5 and Fedora 20/19

# service ntpd start
# chkconfig ntpd on

Wednesday, October 7, 2015

UNIX Basic Commands

LINUX ESSENTIALS



Index

  1. INTRODUCTION
  2. BASICS
  3. UNIX HELP
  4. FINDING THINGS
  5. PERMISSIONS & OWNERSHIP
  6. USEFUL COMMANDS
  7. JOB/PROCESS MANAGEMENT
  8. TEXT VIEWING
  9. TEXT EDITORS
  10. THE UNIX SHELL
  11. SIMPLE SHELL ONE-LINER SCRIPTS
  12. SIMPLE PERL ONE-LINER SCRIPTS
  13. REMOTE COPY
  14. ARCHIVING AND COMPRESSING
  15. SIMPLE INSTALLS
  16. DEVICES
  17. ENVIRONMENT VARIABLES
  18. EXERCISES


  • INTRODUCTION

  • Why UNIX?
    • Multitasking
    • Remote tasking ("real networking")
    • Multiuser
    • Access to shell, programming languages, databases, open-source projects
    • Better performance, less expensive (free), more up-to-date
    • Many more reasons

    How to get access

    UNIX variants
    • UNIX: Solaris, IRIX, HP-UX, Tru64-UNIX, Free's, LINUX, ...
    LINUX distributions
    • RedHat, Debian, Mandrake, Caldera, Slackware, SuSE, ...

    1. BASICS
    2. Syntax for this manual
        Remember the UNIX/LINUX command line is case sensitive!
        "$" indicates start of command
        "#" indicates end of command and start of comment
        The text in green or red monospace font represents the actual command. The "$" and "#" symbols are not part of it. The commands in red emphasize essential information for beginners.
        "<...>" or "my_..." refers to variables and file names that need to be specified by the user. The arrows "<...>" need to be excluded, because they are generic UNIX redirection functions!

      Login from Windows:

      Login from Mac OS-X or LINUX
      • open terminal and type:

      • $ ssh @
        $ user name: ...
        $ password: ...

      Changing password:
        $ passwd # follow instructions

      Orientation
        $ pwd # present working directory
        $ ls # content of pwd
        $ ll # similar as ls, but provides additional info on files and directories
        $ ll -a # includes hidden files (.name) as well
        $ ll -R # lists subdirectories recursively
        $ ll -t # lists files in chronological order
        $ stat # provides all attributes of a file
        $ whoami # shows as who you are logged in
        $ hostname # shows on which machine you are

      Files and directories
        $ mkdir # creates specified directory
        $ cd # switches into specified directory
        $ cd .. # moves one directory up
        $ cd ../../ # moves two directories up (and so on)
        $ cd # brings you to highest level of your home directory
        $ rmdir # removes empty directory
        $ rm # removes file name
        $ rm -r # removes directory including its content, but asks for confirmation, 'f' argument turns confirmation off
        $ mv # renames directories or files
        $ mv # moves file/directory as specified in path
        $ cp # copy file/directory as specified in path (-r to include content in directories)

      Copy and paste
      • Depends on local environment. Usually one of the following methods works:

      • Copy: Ctrl&Shift&c or right/middle mouse click
        Paste: Ctrl&Shift&p or right/middle mouse click

      Handy shortcuts
        $ . # refers to pwd
        $ ~/ # refers to user's home directory
        $ history # shows all commands you have used recently
        $ ! # starts an old command by providing its ID number
        $ up(down)_key # scrolls through command history
        $ TAB # completes path/file_name
        $ SHIFT&TAB # completes command
        $ Ctrl a # cursor to beginning of command line
        $ Ctrl e # cursor to end of command line
        $ Ctrl d # delete character under cursor
        $ Ctrl k # delete line from cursor, content goes into kill buffer
        $ Ctrl y # paste content from Ctrl k

    3. UNIX HELP
      • $ man # general help
        $ man wc # manual on program 'word count' wc
        $ wc --help # short help on wc
        $ info wc # more detailed information system (GNU)
        $ apropos wc # retrieves pages where wc appears
        Online help: SuperMan Pages, Linux Documentation Project (LDP)

    4. FINDING THINGS
    5. Finding files, directories and applications
        $ find -name "*pattern*" # searches for *pattern* in and below current directory
        $ find /usr/local -name "*blast*" # finds file names *blast* in specfied directory
        $ find /usr/local -iname "*blast*" # same as above, but case insensitive
        additional useful arguments: -user , -group , -ctime
        $ find ~ -type f -mtime -2 # finds all files you have modified in the last two days
        $ locate # finds files and dirs that are written into update file
        $ which # location of application
        $ whereis # searches for executeables in set of directories
        $ dpkg -l | grep mypattern # find Debian packages and refine search with grep pattern

      Finding things in files
        $ grep pattern file # provides lines in 'file' where pattern 'appears', if pattern is shell function use single-quotes: '>'
        $ grep -H pattern # -H prints out file name in front of pattern
        $ grep 'pattern' file | wc # pipes lines with pattern into word count wc (see chapter 8); wc arguments: -c: show only bytes, -w: show only words, -l: show only lines; help on regular expressions: $ man 7 regex or man perlre
        $ find /home/my_dir -name '*.txt' | xargs grep -c ^.* # counts line numbers on many files and records each count along with individual file name; find and xargs are used to circumvent the Linux wildcard limit to apply this function on thousands of files.

    6. PERMISSIONS & OWNERSHIP
    7. How does it work
        $ ls -al # shows something like this for each file/dir: drwxrwxrwx
          d: directory
          rwx: read write execute
          first triplet: user permissions (u)
          second triplet: group permissions (g)
          third triplet: world permissions (o)

      To assign write and execute permissions to user and group:
        $ chmod ug+rx my_file

      To remove all permissions from all three user groups:
        $ chmod ugo-rwx my_file
          '+' causes the permissions selected to be added
          '-' causes them to be removed
          '=' causes them to be the only permissions that the file has.
          Example for number system:
        $ chmod +rx public_html/ or $ chmod 755 public_html/

      Change ownership
        $ chown # changes user ownership
        $ chgrp # changes group ownership
        $ chown : # changes user & group ownership

    8. USEFUL UNIX COMMANDS
      • $ df # disk space
        $ free # memory info
        $ uname -a # shows tech info about machine
        $ bc # command-line calculator (to exit type 'quit')
        $ wget ftp://ftp.ncbi.nih.... # file download from web
        $ /sbin/ifconfig # give IP and other network info
        $ ln -s original_filename new_filename # creates symbolic link to file or directory
        $ du -sh # displays disk space usage of current directory
        $ du -sh * # displays disk space usage of individual files/directories
        $ du -s * | sort -nr # shows disk space used by different directories/files sorted by size

    9. JOB/PROCESS MANAGEMENT
      • $ who # shows who is logged into system
        $ w # shows which users are logged into system and what they are doing
        $ ps # shows processes running by user
        $ ps -e # shows all processes on system; try also '-a' and '-x' arguments
        $ ps aux | grep # shows all processes of one user
        $ top # view top consumers of memory and CPU
        $ mtop # displays multicomputer/CPU processes
        $ Ctrl z bg or fg # suspends a process to bring into back- or foreground
        $ Ctrl c # stops an initiated process
        $ kill # Kills specified job; if this doesn't do it, add -9 as argument. Also, type <%1> then .
        $ renice -n # change priority value, which range from 1-19, the higher the value the lower the priority, default is 10

    10. TEXT VIEWING
      • $ less # more versatile text viewer than 'more', 'G' moves to end of text, 'g' to beginning, '/' find forward, '?' find backwards
        $ more # views text, use space bar to browse, hit 'q' to exit
        $ cat # concatenates files and prints content to standard output

    11. TEXT EDITORS
    12. VI and VIM
        Non-graphical (terminal-based) editor. Vi is guaranteed to be available on any system. Vim is the improved version of vi.

      EMACS
        Window-based editor. You still need to know keystroke commands to use it. Installed on all Linux distributions and on most other Unix systems.

      XEMACS
        More sophisticated version of emacs, but usually not installed by default. All common commands are available from menus. Very powerful editor, with built-in syntax checking, Web-browsing, news-reading, manual-page browsing, etc.

      PICO
        Simple terminal-based editor available on most versions of Unix. Uses keystroke commands, but they are listed in logical fashion at bottom of screen.

      VIM MANUAL (essentials marked in red)
      BASICS
        $ vim my_file_name # open/create file with vim
        $ i # INSERT MODE
        $ ESC # NORMAL (NON-EDITING) MODE
        $ : # commands start with ':'
        $ :w # save command; if you are in editing mode you have to hit ESC first!!
        $ :q # quit file, don't save
        $ :q! # exits WITHOUT saving any changes you have made
        $ :wq # save and quit
        $ R # replace MODE
        $ r # replace only one character under cursor
        $ q: # history of commands (from NORMAL MODE!), to reexecute one of them, select and hit enter!
        $ :w new_filename # saves into new file
        $ :#,#w new_filename # saves specific lines (#,#) to new file
        $ :# go to specified line number

      HELP
        $ Useful list of vim commands: Vim Commands Cheat Sheet, VimCard, Vim Basics
        $ vimtutor # open vim tutorial from shell
        $ :help # opens help within vim, hit :q to get back to your file
        $ :help # opens help on specified topic
        $ |help_topic| CTRL-] # when you are in help this command opens help topic specified between |...|, CTRL-t brings you back to last topic
        $ :help CTRL-D # gives list of help topics that contain key word
        $ : # like in shell you get recent commands!!!!

      MOVING AROUND IN FILE
        $ $ # moves cursor to end of line
        $ A # same as $, but switches to insert mode
        $ 0 (zero) # moves cursor to beginning of line
        $ CTRL-g # shows at status line filename and the line you are on
        $ SHIFT-G # brings you to bottom of file, type line number (isn't displayed) then SHIFT-G # brings you to specified line#

      DISPLAY
        WRAPPING AND LINE NUMBERS
        $ :set nowrap # no word wrapping, :set wrap # back to wrapping
        $ :set number # shows line numbers, :set nonumber # back to no-number mode

      WORKING WITH MANY FILES & SPLITTING WINDOWS
        $ vim *.txt # opens many files at once; ':n' switches between files
        $ :wall or :qall # write or quit all open files
        $ vim -o *.txt # opens many files at once and displays them with horizontal split, '-O' does vertical split
        $ :args *.txt # places all the relevant files in the argument list $ :all # splits all files in the argument list (buffer) horizontally $ CTRL-w # switch between windows
        $ :split # shows same file in two windows
        $ :split # opens second file in new window
        $ :vsplit # splits windows vertically, very useful for tables, ":set scrollbind" let's you scroll all open windows symultaneously
        $ :close # closes current window
        $ :only # closes all windows except current one

      SPELL CHECKING & Dictionary
        $ aspell -c # shell command
        $ aspell -l # shell command
        $ :! dict # meaning of word
        $ :! wn 'word' -over # synonyms of word

      PRINTING FILE
        $ :ha # prints entire file
        $ :#,#ha # prints specified lines: #,#

      MERGING/INSERTING FILES
        $ :r # inserts content of specified file after cursor

      UNDO/REDO
        $ u # undo last command
        $ U # undo all changes on current line
        $ CTRL-R # redo one change which was undone

      DELETION/CUT (switch to NORMAL mode)
        $ x # deletes what is under cursor
        $ dw # deletes from curser to end of word including the space
        $ de # deletes from curser to end of word NOT including the space
        $ cw # deletes rest of word and lets you then insert, hit ESC to continue with NORMAL mode
        $ c$ # deletes rest of line and lets you then insert, hit ESC to continue with with NORMAL mode
        $ d$ # deletes from cursor to the end of the line
        $ dd # deletes entire line
        $ 2dd # deletes next two lines, continues: 3dd, 4dd and so on.

      PUT (PASTE)
        $ p # uses what was deleted/cut and pastes it behind cursor

      COPY & PASTE
        $ yy # copies line, for copying several lines do 2yy, 3yy and so on
        $ p # pastes clipboard behind cursor

      SEARCH IN FILE (most regular expressions work)
        $ /my_pattern # searches for my_pattern downwards, type n for next match
        $ ?my_pattern # seraches for my_pattern upwards, type n for next match
        $ :set ic # switches to ignore case search (case insensitive)
        $ :set hls # switches to highlight search (highlights search hits)

      REPLACE WITH REGULAR EXPRESSIONS (great intro: A Tao of Regular Expressions)
        $ :s/old_pat/new_pat/ # replaces first occurence in a line
        $ :s/old_pat/new_pat/g # replaces all occurence in a line
        $ :s/old_pat/new_pat/gc # add 'c' to ask for confirmation
        $ :#,#s/old_pat/new_pat/g # replaces all occurence between line numbers: #,#
        $ :%s/old_pat/new_pat/g # replaces all occurence in file
        $ :%s/\(pattern1\)\(pattern2\)/\1test\2/g # regular expression to insert, you need here '\' in front of parentheses (<# Perl)
        $ :%s/\(pattern.*\)/\1 my_tag/g # appends something to line containing pattern (<# .+ from Perl is .* in VIM)
        $ :%s/\(pattern\)\(.*\)/\1/g # removes everything in lines after pattern
        $ :%s/\(At\dg\d\d\d\d\d\.\d\)\(.*\)/\1\t\2/g # inserts tabs between At1g12345.1 and Description
        $ :%s/\n/new_pattern/g #Replaces return signs
        $ :%s/pattern/\r/g #Replace pattern with return signs!!
        $ :%s/\(\n\)/\1\1/g # insert additional return signs
        $ :%s/\(^At\dg\d\d\d\d\d.\d\t.\{-}\t.\{-}\t.\{-}\t.\{-}\t\).\{-}\t/\1/g # replaces content between 5th and 6th tab (5th column), '{-}' turns off 'greedy' behavior
        $ :#,#s/\( \{-} \|\.\|\n\)/\1/g # performs simple word count in specified range of text
        $ :%s/\(E\{6,\}\)/\1<\/font>/g # highlight pattern in html colors, here highlighting of >= 6 occurences of Es
        $ :%s/\([A-Z]\)/\l\1/g # change uppercase to lowercase, '%s/\([A-Z]\)/\u\1/g' does the opposite
        $ :g/my_pattern/ s/\([A-Z]\)/\l\1/g | copy $ # uses 'global' command to apply replace function only on those lines that match a certain pattern. The 'copy $' command after the pipe '|' prints all matching lines at the end of the file.
        $ :args *.txt | all | argdo %s/\old_pat/new_pat/ge | update # Command 'args' places all relevant files in the argument list (buffer); 'all' displays each file in separate split window; command 'argdo' applies replacement to all files in argument list (buffer); flag 'e' is necessary to avoid stop at error messages for files with no matches; command 'update' saves all changes to files that were updated.

      MATCHING PARENTHESES SEARCH
        - place curser on (, [ or { and type % # curser moves to matching parentheses

      HTML EDITING
        -Convert text file to html format:
        $ :runtime! syntax/2html.vim # run this command with open file in Vim

      SHELL COMMAND IN VIM
        $ :! # executes any shell command, hit to return
        $ :sh # switches window to shell, 'exit' switches back to vim

      USING VIM AS TABLE EDITOR
        $ v # starts visual mode for selecting characters
        $ V # starts visual mode for selecting lines
        $ CTRL-V # starts visual mode for selecting blocks (use CTRL-q in gVim under Windows). This allows column-wise selections and operations like inserting and deleting columns. To restrict substitude commands to a column, one can select it and switch to the command-line by typing ':'. After this the substitution sytax for a selected block looks like this: '<,'>s///.
        $ :set scrollbind # starts simultaneous scrolling of 'vsplitted' files. To set to horizontal binding of files, use command ':set scrollopt=hor' (after first one). Run all these commands before the ':split' command.
        $ :AlignCtrl I= \t then :%Align # This allows to align tables by column separators (here '\t') when the Align utility from Charles Campbell's is installed.
        To sort table rows by selected lines or block, perform the visual select and then hit F3 key. The rest is interactive. To enable this function one has to include in the .vimrc file from Gerald Lai the Vim sort script.

      MODIFY VIM SETTINGS (in file .vimrc)
        - see last chapter of vimtutor (start from shell)
        - useful .vimrc sample
        - when vim starts to respond very slowly then one may need to delete the .viminf* files in home directory

    13. THE UNIX SHELL
    14. When you log into UNIX/LINUX the system starts a program called SHELL. It provides you with a working environment and interface to the operating system. Usually there are many different shell programs installed.
        $ finger # shows which shell you are using
        $ chsh -l # gives list of shell programs available on your system (does not work on all UNIX variants)
        $ # switches to different shell

      STDIN, STDOUT, STDERR, REDIRECTORS, OPERATORS & WILDCARDS (more on this @ LINUX HOWTOs)
        By default, many UNIX commands read from standard input (STDIN) and send their output to standard out (STDOUT). You can redirect them by using the following commands:
        $ file* # * is wildcard to specify many files
        $ ls > file # prints ls output into specified file
        $ command < my_file # uses file after '<' as STDIN
        $ command >> my_file # appends output of one command to file
        $ command | tee my_file # writes STDOUT to file and prints it to screen; alternative way to do this:
        $ command > my_file; cat my_file
        $ command > /dev/null # turns off progress info of applications by redirecting their output to /dev/null
        $ grep my_pattern my_file | wc # Pipes (|) output of 'grep' into 'wc'
        $ grep my_pattern my_non_existing_file 2 > my_stderr # prints STDERR to file

      Useful shell commands
        $ cat > # concatenate files in output file 'cat.out'
        $ paste > # merges lines of files and separates them by tabs (useful for tables)
        $ cmp # tells you whether two files are identical
        $ diff # finds differences between two files
        $ head - # prints first lines of a file
        $ tail - # prints last lines of a file
        $ split -l # splits lines of file into many smaller ones
        $ csplit -f out fasta_batch "%^>%" "/^>/" "{*}" # splits fasta batch file into many files at '>'
        $ sort # sorts single file, many files and can merge (-m) them, -b ignores leading white space, ...
        $ sort -k 2,2 -k 3,3n input_file > output_file # sorts in table column 2 alphabetically and column 3 numerically, '-k' for column, '-n' for numeric
        $ sort input_file | uniq > output_file # uniq command removes duplicates and creates file/table with unique lines/fields
        $ join -1 1 -2 1 # joins two tables based on specified column numbers (-1 file1, 1: col1; -2: file2, col2). It assumes that join fields are sorted. If that is not the case, use the next command:
        $ sort table1 > table1a; sort table2 > table2a; join -a 1 -t "`echo -e '\t'`" table1a table2a > table3 # '-a ' prints all lines of specified table! Default prints only all lines the two tables have in common. '-t "`echo -e '\t'`" ->' forces join to use tabs as field separator in its output. Default is space(s)!!!
        $ cat my_table | cut -d , -f1-3 # cut command prints only specified sections of a table, -d specifies here comma as column separator (tab is default), -f specifies column numbers.
        $ grep and egrep # see chapter 4

    15. SIMPLE SHELL ONE-LINER SCRIPTS
    16. Useful One-Liners (script download)
        $ for i in *.input; do mv $i ${i/name\.old/name\.new}; done # renames file name.old to name.new
        - To test things first, insert 'echo' between 'do mv' (above).
        $ for i in *.input; do ./application $i; done # runs application in loops on many input files
        $ for i in *.input; do fastacmd -d /data/../database_name -i $i > $i.out; done # runs fastacmd in loops on many *.input files and creates *.out files
        $ for i in *.pep; do target99 -db /usr/../database_name -seed $i -out $i; done # runs SAM's target99 on many input files
        $ for j in 0 1 2 3 4 5 6 7 8 9; do grep -iH *$j.seq; done # searches in > 10,000 files for pattern and prints occurences together with file names.
        $ for i in *.pep; do echo -e "$i\n\n17\n33\n\n\n" | ./tmpred $i > $i.out; done # example of how to run an interactive application (tmpred) that asks for file name input/output
        $ for i in *.fasta1; do blast2 -p blastp -i $i -j ${i/_*fasta1/_*fasta2} >> my_out_file; done # runs BLAST2 for all *.fasa1/*.fasta2 file pairs in the order specified by file names and writes results into one file. This example uses two variables in a for loop. The content of the second variable gets specified in each loop by a replace function.
        $ for i in *.fasta; do for j in *.fasta; do blast2 -p blastp -F F -i $i -j $j >> my_out_file; done; done; # runs BLAST2 in all-against-all mode and writes results into one file; '-F F' turns low-complexity filter off

      How to write a script
        - create file which contains in first line:
        #!/bin/bash
        - place shell commands in file
        - run to make it executable
        - run shell script like this: ./my_shell_script
        - when you place it into /usr/local/bin you only type its name from any user account

    17. SIMPLE PERL ONE-LINER SCRIPTS
    18. Useful One-Liners
        $ perl -p -i -w -e 's/pattern1/pattern2/g' input_file # replace something (e.g. return signs) in file using regular expressions; use $1 to backreference to pattern placed in parentheses
        '-p' lets perl know to write program; '-i.bak' creates backup file *.bak, only -i doesn't; '-w' turns on warnings; '-e' executeable code follows
        $ perl -ne 'print if (/my_pattern1/ ? ($c=1) : (--$c > 0)) ; print if (/my_pattern2/ ? ($d = 1) : (--$d > 0))' my_infile > my_outfile # parses lines that contain pattern1 and pattern2
        following lines after pattern can be specified in '$c=1' and '$d=1'; for OR function use this syntax: '/(pattern1|pattern2)/'

    19. REMOTE COPY: WGET, SCP and NCFTP
    20. WGET (file download from the www)
        $ wget ftp://ftp.ncbi.nih.... # file download from www; add option '-r' to download entire directories

      SCP (secure copy between machines)
        General syntax
        $ scp source target # Use form 'userid@machine_name' if your local and remote user ids are differnt. If they are the same you can use only 'machine_name'.

        Examples
        Copy file from Server to Local Machine (type from local machine prompt):
        $ scp user@remote_host:file.name . # '.' copies to pwd, you can specify here any directory, use wildcards to copy many files at once.
        Copy file from Local Machine to Server:
        $ scp file.name user@remote_host:~/dir/newfile.name
        Copy entire directory from Server to Local Machine (type from local machine prompt):
        $ scp -r user@remote_host:directory/ ~/dir
        Copy entire directory from Local Machine to Server (type from local machine prompt):
        $ scp -r directory/ user@remote_host:directory/
        Copy between two remote hosts (e.g. from bioinfo to cache):
        similar as above, just be logged in one of the remote hosts:
        $ scp -r directory/ user@remote_host:directory/

      NICE FTP
        $ open ncftp
        $ ncftp> open ftp.ncbi.nih.gov
        $ ncftp> cd /blast/executables
        $ ncftp> get blast.linux.tar.Z (skip extension: @)
        $ ncftp> bye

    21. ARCHIVING AND COMPRESSING
    22. Archiving and compressing
        $ tar -cvf my_file.tar mydir/ # Builds tar archive of files or directories. For directories, execute command in parent directory. Don't use absolute path.
        $ tar -czvf my_file.tgz mydir/ # Builds tar archive with compression of files or directories. For directories, execute command in parent directory. Don't use absolute path.

      Viewing Archives
        $ tar -tvf my_file.tar
        $ tar -tzvf my_file.tgz

      Extracting
        $ tar -xvf my_file.tar
        $ tar -xzvf my_file.tgz
        $ gunzip my_file.tar.gz # or unzip my_file.zip, uncompress my_file.Z, or bunzip2 for file.tar.bz2
        $ find -name '*.zip' | xargs -n 1 unzip # this command usually works for unziping many files that were compressed under Windows
        try also:
          $ tar zxf blast.linux.tar.Z
          $ tar xvzf file.tgz
        options:
          f: use archive file
          p: preserve permissions
          v: list files processed
          x: exclude files listed in FILE
          z: filter the archive through gzip

    23. SIMPLE INSTALLS
    24. Systems-wide installations
        Installations for systems-wide usage are the responsibility of system administrator
        To find out if an application is installed, type:
        $ which
        $ whereis # searches for executeables in set of directories, doesn't depend on your path
        Most applications are installed in /usr/local/bin or /usr/bin. You need root permissions to write to these directories.
        Perl scripts go into /usr/local/bin, Perl modules (*.pm) into /usr/local/share/perl/5.8.0/. To copy executables in one batch, use command: cp `find -perm -111 -type f` /usr/local/bin

      Applications in user accounts
        Create a new directory, download application into this directory, unpack it (see chapter 13) and follow package-specific installation instructions.
        Usually you can then already run this application when you specify its location e.g.: /home/user/my_app/blastall.
        If you want you can add this directory to your PATH by typing from this directory:
        $ PATH=.:$PATH; export PATH # this allows you to run application by providing only its name; when you do echo $PATH you will see .: added to PATH.

      Intstallation of RPMs
        $ rpm -i application_name.rpm
        To check which version of RPM package is installed, type:
        $ rpm --query
        Help and upgrade files for RPMs can be found at http://rpmfind.net/.

      Installation of Debian packages
        Check whether your application is available at: http://www.debian.org/intro/about, then you type (no download):
        $ apt-cache search phylip #searches for application "phylip" from command line
        $ apt-cache show phylip #provides description of program
        $ apt-get install phylip # example for phylip install, manuals can be found in /usr/doc/phylip/, use zless or lynx to read documentation (don't unzip).
        $ apt-get update # do once a month do update Debian packages
        $ apt-get upgrade -u # to upgrade after update from above
        $ dpkg -i # install data package from local package file (e.g. after download)
        $ aptitude # Debian package manageing interface (Ctrl-t opens menues)
        $ aptitude search vim # search for packages on system and in Debian depositories

    25. DEVICES
    26. Mount/unmount usb/floppy/cdrom
        $ mount /media/usb
        $ umount /media/usb
        $ mount /media/cdrom
        $ eject /media/cdrom
        $ mount /media/floppy

    27. ENVIRONMENT VARIABLES
      • $ xhost user@host # adds X permissions for user on server.
        $ echo DISPLAY # shows current display settings
        $ export (setenv) DISPLAY=:0 # change environment variable
        $ unsetenv DISPLAY # removes display variable
        $ printenv # prints all environment variables
        $ $PATH # list of directories that the shell will search when you type a command
        - You can edit your default DISPLAY setting for your account by adding it to file .bash_profile.

    28. EXERCISES
    29. Exercise 1
      1. Download proteome of Halobacterium spec. from ftp://ftp.ncbi.nih.gov/genbank/genomes/Bacteria/Halobacterium_sp/AE004437.faa (use wget or web browser for download)
      2. How many predicted proteins are there?
        • $ grep '>' AE004437.faa | wc
      3. How many proteins contain the pattern "WxHxxH[1-2]"?
        • $ egrep 'W.H..H{1,2}' AE004437.faa | wc
      4. Use the find function (/) in 'less' to fish out the proteins containing this pattern or more elegantly do it with awk:
        • $ awk --posix -v RS='>' '/W.H..(H){1,2}/ { print ">" $0;}' AE004437.faa | less
      5. Create a BLASTable database with formatdb
        • $ formatdb -i AE004437.faa -p T -o T
          '-p F' for nucleotide and '-p T' for protein databases
      6. Generate list of sequence IDs for above pattern match result and retrieve its sequences with fastacmd from formatted database
        • $ fastacmd -d AE004437.faa -i my_IDs > seq
      7. Generate several lists of sequence IDs from various pattern match results and retrieve their sequences in one step using the fastacmd in for loop
        • $ for i in *.my_ids; do fastacmd -d AE004437.faa -i $i > $i.out; done
      8. Run blastall with a few proteins against newly created database or against Halobacterium or UniProt database (/data/UNIPROT/blast/uniprot)
        • $ blastall -p blastp -i input.file -d AE004437.faa -o blastp.out -e 1e-6 -v 10 -b 10 &
      9. Parse blastall output into Excel spread sheet:
        • a) using biocore parser
          $ blastParse -c -i -o
          b) using BioPerl parser
          $ bioblastParse.pl blast.out
      10. Run HMMPFAM search with above proteins against Pfam database
        • $ hmmpfam -E 0.1 --acc -A0 /data/PFAM/Pfam_ls input.file > output.pfam
          Parse result with BioPerl parser
          $ hmmSummary output.pfam > hmm.summary
      Exercise 2
      1. Split sample fasta batch file with csplit (use sequence file from exercise 1).
      2. Concatenate single fasta files from (1) to one batch file.
      3. BLAST two related sequences, retrieve the result in table format and use join to identify common hit IDs in the two tables.
      Exercise 3
      1. write a shell script that executes several BLAST searches at once:
        • #!/bin/sh
          blastall -p blastp -d /.../my_database -i /.../my_input -o my_out -e 1e-6 -v 10 -b 10 &
          blastall -p blastp -d /.../my_database -i /.../my_input -o my_out -e 1e-6 -v 10 -b 10 &
      Exercise 4
      1. Create multiple alignment with ClustalW (e.g. use sequences with 'W.H..HH' pattern)
        • $ clustalw my_fasta_batch
      Exercise 5
      1. Reformat alignment into PHYILIP format using 'seqret' from EMBOSS
        • $ seqret clustal::my_align.aln phylip::my_align.phylip
      Exercise 6
      1. Create neighbor-joining tree with PHYLIP
        • $ cp my_align.phylip infile
          $ phylip protdist # creates distance matrix
          $ cp outfile infile
          $ phylip neighbor # use default settings
          $ cp outtree intree
          $ phylip retree # displays tree and can use midpoint method for defining root of tree, my typical command sequence is: 'N' 'Y' 'M' 'W' 'R' 'R' 'X'
          $ cp outtree my_tree.dnd
          View your tree in TreeBrowse or open it in TreeView

    Tuesday, September 11, 2012

    dd command to create an ISO image

    Put CD into CDROM
    Do not mount CD. Verify if cd is mounted or not with mount command:
        # mount
    If cd was mouted automatically unmount it with umount command:
        # umount /dev/cdrom
    OR
        # umount /mnt/cdrom

    Create CD-ROM ISO image with dd command:
        # dd if=/dev/cdrom of=/tmp/cdimg1.iso

    Where,
    # if=/dev/cdrom: Read from /dev/cdrom (raw format)  of=/tmp/cdimg1.iso: write to FILE cdimg1.iso

    i.e. create an ISO image

    TOP command - Useful switch

    TOP command useful switch--

    1> Select - Press 'f' to select unselected parameteres ie SWAP.
    1> Sort - To sort top output by any column, Press O (upper-case O)
    2> Kill a Task Without Exiting From Top – Press k
    3> Renice a Unix Process Without Exiting From Top – Press r
    4> Display Selected User in Top Output Using top -u
    5> Display All CPUs / Cores in the Top Output – Press 1 (one)
    6> Change Refresh Interval - press d , default is 3 second
    7> Highlight Running Processes in the Linux Top Command Output – Press z or b
    8> Display Absolute Path of the Command and its Arguments – Press c

    Saturday, August 9, 2008

    Installing and configuring Lock and RCS scritp

    • Install RCS package form Sun freeware. “rcs-5.7-sol10-sparc-local”
    • Install following pkg’s
      • libgcc-3.4.6-sol10-sparc-local
      • tcl-8.5.3-sol10-sparc-local
      • libgcc-3.3-sol10-sparc-local
      • expect-5.43.0-sol10-sparc-local
    copy lock, lock.exc, lockadm under /usr/bin
    chown bin:bin /usr/bin/lock*
    # cd /var/etc/
    # mkdir RCS
    // Put make.conf under revision control
    # ci -i make.conf
    RCS/make.conf,v <-- make.conf
    enter description, terminated with single '.' or end of file:
    NOTE: This is NOT the log message!
    >> make.conf - custom settings for portage
    >> .
    initial revision: 1.1
    done
    // The file is now in RCS, so put it back in /etc for reading
    # co make.conf
    Notice that make.conf is now under revision control and is read-only. To edit the file, you need to perform the following steps:
    Code Listing 2: Editing a file under revision control
    // Checkout the file with locking enabled
    # co -l make.conf
    RCS/make.conf,v --> make.conf
    revision 1.1 (locked)
    done
    // Edit the file
    # ${EDITOR} make.conf
    // Check the file back in and unlock it.
    # ci -u make.conf ( some time we use this command to unlock the file locked by root)
    RCS/make.conf,v <-- make.conf
    new revision: 1.2; previous revision: 1.1
    enter log message, terminated with single '.' or end of file:
    >> changed DISTDIR to use a drive with more space
    >> . done
    ci(1) and co(1)
    ci(1) and co(1) are the commands used to check files in and out of their RCS archives. The ci(1) command may also be used to a check a file both in and out of an archive. In their simplest forms, ci(1) and co(1) take only the name of the working file.
    ci name_of_work_file
    and
    co name_of_work_file
    The command form
    ci -l name_of_work_file
    checks in the file with locking enabled, and
    co -l name_of_work_file
    is performed automatically. That is, ci -l checks the file out again with locking enabled.
    ci -u name_of_work_file
    checks the file into the archive, and checks it out again with locking disabled. In all cases, the user is prompted for a log message.
    ci(1) will also create a RCS archive if one does not exist already.
    bash-3.00# /usr/local/bin/rlog aliases
    RCS file: RCS/aliases,v
    Working file: aliases
    head: 1.6
    branch:
    locks: strict
    access list:
    symbolic names:
    keyword substitution: kv
    total revisions: 6; selected revisions: 6
    description:
    Aliases file
    ----------------------------
    revision 1.6
    date: 2008/07/16 21:00:44; author: tsaini; state: Exp; lines: +0 -1
    Removed the entry for tsaini
    ----------------------------
    revision 1.5
    date: 2008/07/16 15:29:31; author: tsaini; state: Exp; lines: +1 -1
    *** empty log message ***
    ----------------------------
    revision 1.4
    date: 2008/07/15 21:50:58; author: hbanwait; state: Exp; lines: +1 -1
    *** empty log message ***
    =============================================================================
    bash-3.00#

    Viewing Previous Versions of a File

    RCS keeps track of all revisions of a file. You can view a previous version, say version 1.6, of a file (named FILE) by checking out the old version and redirecting it to standard output.
    bash-3.00# /usr/local/bin/co -p -r1.5 RCS/aliases,v
    You can see the differences between revisions with the rcsdiff command. This example shows the differences between version 1.5 and 1.6.
    bash-3.00# /usr/local/bin/rcsdiff -r1.6 -r1.5 RCS/aliases,v
    ===================================================================
    RCS file: RCS/aliases,v
    retrieving revision 1.6
    retrieving revision 1.5
    diff -r1.6 -r1.5
    3833a3834
    > riskmapper: tsaini
    bash-3.00#

    Now we can use Lock script to lock the file and edit it. Lock script is customized script to work with rcs.
    Lockadm to mange the files.
    bash-3.00# lockadm -f ypservers -a -u tsaini
    RCS file: RCS/ypservers,v
    done
    bash-3.00# lockadm -f ypservers -l
    The following users are on the access list for ypservers:
    tsaini
    bash-3.00# lockadm -f ypservers –l
    or if you do # lock filename it will ask you to put that file in RCS