Mahesh's Blog

" It's hard to beat a person who believes in his or her own strength. And I believe in mine."

Wednesday, January 14, 2015

Useful LINUX commands


    grep command: Searches for given string in files
        grep -i "search_text" file_name
        grep -ilrn "search_text" *             (To search in all files)
        Attributes: i for ignore case, l for list names of files, r for searching recursively, n for line number where text is matched in the file
    find command: Finds files using file-name
        find -iname "MyTextFile.c"
        find -iname "MyTextFile.c" -exec md5sum {} \;    :Executes commands on files found by the find command
        find ~ -empty                     :Finds all  empty files in home directory
    pwd command: pwd is Present working directory. It prints the current directory.
    cd command: It is used to change the directory.
        Use “cd -” to toggle between the last two directories
        Use “shopt -s cdspell” to automatically correct mistype directory names on cd
    diff command: compares two files
        diff -w file1.txt file2.txt
    tar command examples:
        tar cvf archive_name.tar dirname/     :Creates a new tar archive
        tar xvf archive_name.tar   : Extracts from existing tar archive
        tar tvf archive_name.tar   : view an existing tar archive
    gzip command examples
        gzip file.txt : creates a *.gz compressed file
        gzip -d file.txt.gz  : Uncompress a *.gz file
        gzip -l *.gz  : Displays the compression ratio of the compressed file
    bzip2 command examples
        bzip2 file.txt  : creates a *.bz2 compressed file
        bzip2 -d file.txt.bz2  : uncompresses a *.bz2 file
    unzip command examples
        unzip test.zip   : Extracts the zipped file
        unzip -l test.zip  : views the content of zipped file without unzipping
    ssh commands: It is used to work remotely
        ssh -l kamal remotehost.com     : Login to remote gost
        ssh -l kamal 10.201.42.12
        ssh -v -l kamal remotehost.com   : Debug ssh client
        ssh -V : Display ssh client version
    ftp command examples
        ftp IP/hostname    : connects to a remote server
        ftp> mget *.html  : Download file from that server
        ftp> mls *.html -   : views the file names located on the remote server
    wget command :
        wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.1.tar.gz : to download any file from internet
        wget -O taglist.zip http://www.vim.org/scripts/download_script.php?src_id=7701  :  Download and store it
    vim command examples
        vim +14 file.txt   : Go to the 14th line of file
        vim +/search_text file.txt  : goto the first match of the specified search text
        vim -R /etc/passwd  : Opent he file in the read only mode
    sort command examples
        sort nameList.txt :  Sorts a file in ascending order
        sort -r nameList.txt :  Sorts a file in descending order
        sort -t: -k 3n /etc/passwd | more :  Sorts passwd file by third field
    xargs command examples
        ls *.jpg | xargs -n1 -i cp {} /external-drive/directory    : Copy all images to external drive
        find / -name *.jpg -type f -print | xargs tar -cvzf allImages.tar.gz    : Search all jpg images in the system and archive it.
        cat url-list.txt | xargs wget –c  :  Downloads all the URLs mentioned in the url-list.txt file
    ls command examples
        ls -lh :  Displays filesize in KB / MB
        ls -ltr  :  Orders Files Based on Last Modified Time
        ls -F   : Visual Classification of Files
    sed command examples
        sed 's/.$//' filename  :Converts the DOS file format to Unix file format (removes \r or \n)
        sed -n '1!G;h;$p' file.txt   : Prints file content in reverse order
        sed '/./=' file.txt | sed 'N; s/\n/ /'  :  Adds line number for all non-empty-lines in the file
    awk command examples
        awk '!($0 in array) { array[$0]; print }' temp   : Removes duplicate lines
        awk -F ':' '$3==$4' passwd.txt   ;  Prints all lines from /etc/passwd that has the same uid and gid
        awk '{print $2,$5;}' file.txt   : Prints only specific field from a file.
    shutdown command examples
        shutdown -h now   : Shutdown the system and turn the power off immediately.
        shutdown -h +10   : Shutdown the system after 10 minutes.
        shutdown -r now    : Reboot the system
        shutdown -Fr now  : Force the filesystem check during reboot.
    crontab command examples
        crontab -u kamal -l  : View crontab entry for a specific user
    service command examples : Service commands are used to run the system V init scripts. i.e Instead of calling the scripts located in the /etc/init.d/ directory with their full path, we can use the service command.
        service ssh status  : checks the service status
        service --status-all : Check the status of all the services.
        service ssh restart :  Restart a service
    ps command :  It is used to display information about the processes running in the system.
        ps -ef | more  : view the current running processes
        ps -efH | more  :  To view current running processes in a tree structure. H means process hierarchy.
    free command: It is used to display the free, used, swap memory available in the system.
        free
        free -g :  If you want to quickly check how many GB of RAM your system has use the -g option. -b option displays in bytes, -k in kilo bytes, -m in mega bytes.
        free -t  : use this if you want to see a total memory ( including the swap)
    top command: It displays the top processes in the system, by default sorted by cpu usage. To sort top output by any column, press O (upper-case O) , which will display all the possible columns that you can sort by.
        top
        top -u oracle : To display only the processes that belong to a particular user use -u option. This command will show only the top processes that belongs to oracle user.
    df command:
        df -k  : displays the file system disk space usage. By default df -k displays output in bytes.
        df -h :  displays output in human readable form. i.e size will be displayed in GB’s.
        df -T : display type of file system.
    kill command: It is used to terminate a process. First get the process id using ps -ef command, then use kill -9 to kill the running LINUX process. You can also use killall, pkill, xkill to terminate a unix process.
        ps -ef | grep vim
        kill -9 7243
    rm command : Removes a file
        rm -i filename.txt  :  Get confirmation before removing the file.
        rm -i file*  :  Print the filename and get confirmation before removing the file.
        rm -r example  : It recursively removes all files and directories under the example directory. This also removes the example directory itself.
    cp command : Used for copying files from source to destination
        cp -p file1 file2 : Copy file1 to file2 preserving the mode, ownership and timestamp.
        cp -i file1 file2 : Copy file1 to file2. if file2 exists prompt for confirmation before overwritting it.
    mv command: used to rename a file / folder
        mv -i file1 file2 :  Rename file1 to file2. if file2 exists prompt for confirmation before overwritting it.
        mv -f file1 file2 : Rename file1 to file2. if file2 exists overwrite it without prompting for confirmation
        mv -v file1 file2 : It will print what is happening during file rename, verbose output
    cat command  : used to view the file
        cat file1
        cat file1 file2  : view multiple files at the same time. It prints the content of file1 followed by file2 to stdout.
        cat -n /etc/test.txt  :  It will prepend the line number to each line of the output while displaying the file.
    mount command
        To mount a file system, we should first create a directory and mount it:
            mkdir /newDir
            mount /dev/sdb1 /newDir
        We can also add this to the fstab for automatic mounting. i.e Anytime system is restarted, the filesystem will be mounted.
            /dev/sdb1 /newDir ext2 defaults 0 2
    chmod command: chmod command is used to change the permissions for a file or directory.
        chmod ug+rwx file.txt   ; Give full access (read, write and execute) to user and group on a specific file.
        chmod g-rwx file.txt     : Revoke all access (read, write and execute) for the group on a specific file.
        chmod -R ug+rwx file.txt   :   Apply the file permissions to all the files in the sub-directories.
    chown command: change the owner and group of a file
        chown oracle:dba dbora.sh  :  To change owner to oracle and group to db on a file. i.e Change both owner and group at the same time.
        chown -R oracle:dba /home/oracle   : change the owner recursively
    passwd command : used to change the password through command line
        passwd : User can change their password using this command. It will ask for current password.
        passwd USERNAME : Super user can use passwd command to reset others password. This will not prompt for current password of the user.
        passwd -d USERNAME  :  Remove password for a specific user. Root user can disable password for a specific user. Once the password is disabled, the user can login without entering the password.
    uname command: It displays important information about the system such as — Kernel name, Host name, Kernel release number, Processor type, etc.
        uname -a
    su command
        su - USERNAME  : Switch to a different user account using su command. Super user can switch to any other user without entering their password.
        su - raj -c 'ls' :  Execute a single command from a different account name. In the following example, john can execute the ls command as raj username. Once the command is executed, it will come back to john’s account.
        su -s 'SHELLNAME' USERNAME :  Login to a specified user account, and execute the specified shell instead of the default shell.
    mkdir command: to create directories
        mkdir ~/myDir  : creates a directory called myDir under home directory.
        mkdir -p dir1/dir2/dir3/dir4/   :  It creates nested directories using. If any of these directories exist already, it will not display any error. If any of these directories doesn’t exist, it will create them.
    ifconfig command: used to view or configure a network interface on the Linux system, same as ipconfig in windows.
        ifconfig -a  :  View all the interfaces along with status.
        Start or stop a specific interface using up and down command as below.
            ifconfig eth0 up
            ifconfig eth0 down
    ping command :
        ping google.com : Ping a remote host.
        ping -c 5 google.com  :  Ping a remote host by sending only 5 packets.
    whereis command
        whereis ls    :  When wewant to find out where a specific Unix command exists (for example, where does ls command exists?), we can execute this command.
        whereis -u -B /tmp -f lsmk   :   When you want to search an executable from a path other than the whereis default path, you can use -B option and give path as argument to it. This searches for the executable lsmk in the /tmp directory, and displays it, if it is available.
    whatis command : It displays a single line description about a command.
        whatis ls
        whatis ifconfig
    man command : Displays the man page (manual page) of a specific command
        man crontab
    locate command:  Using locate command we can quickly search for the location of a specific file (or group of files). Locate command uses the database created by updatedb.
        locate crontab
    tail command
        tail file.txt    :   Print the last 10 lines of a file by default.
        tail -n N file.txt   :   Print N number of lines from the file named file.txt
        tail -f log-file  :  View the content of the file in real time using tail -f. This is useful to view the log files, that keeps growing. The command can be terminated using CTRL-C
    less command: less is very efficient while viewing huge log files, as it doesn't need to load the full file while opening. After opening file through less, following command is very useful:
        CTRL+F – forward one window
        CTRL+B – backward one window
    fg command : This command resumes execution of a suspended process
    date command :
        date -s "01/13/2015 13:55:14"   :  Set the system date
        Once you’ve changed the system date, you should syncronize the hardware clock with the system date as shown below.
            hwclock –systohc
            hwclock --systohc –utc
    mysql command
        mysql -u root -p -h 192.160.10.2  : To connect to a remote mysql database. This will prompt for a password.
        mysql -u root -p  :  To connect to a local mysql database. If you want to specify the mysql root password in the command line itself, enter it immediately after -p (without any space).
    yum command
        yum install httpd : To install apache using yum.
        yum update httpd  :  To upgrade apache using yum.
        yum remove httpd :  To uninstall/remove apache using yum.
    rpm command
        rpm -ivh httpd-2.2.3-22.0.1.el5.i386.rpm   :  To install apache using rpm.
        rpm -uvh httpd-2.2.3-22.0.1.el5.i386.rpm  :  To upgrade apache using rpm.
        rpm -ev httpd  :  To uninstall/remove apache using rpm.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home