Tuesday, 4 February 2014

Useful Linux Commands

Sometimes we need to work on Linux environment during development specially on Linux box hosting the server.As we know Linux is bundled with lots of useful commadline features, file handling is one of them.Here is small list of commands which I feel very useful while working on Linux.

Note : Command syntax/example given below are based on requirements/assignment I worked upon.Every command below may have multiple options,which can be used in different combination based on scenarios(requirement).For example I am redirecting output to a file which can also be displayed on console.

      1.  Secure File Copy.

          Using this feature you can securely copy file from one machine to another machine without login into any of the machine. 
            Syntax : scp user1@machine1:Path of source file user2@machine2:Path of the destination directory
            Example :scp admin@192.168.0.101:/opt/file/Test.jar jboss@192.168.0.109:/opt/jboss/app/lib

      2.  Fetching the specific line range from file and write result to another file.

           Syntax : sed -n 'startLineNumber,EndLineNumberp' fileToBeSearch > resultFile
           Example : sed -n '1,500p' Data.csv > SmallData.csv
 
      3. Fetching the line containing given text from file.
          Syntax :grep searchText SourceFile > FileContainingResultData
          Example : Search For single String
            grep nike BrandData.csv > NikeData.csv

          Example : Search For 2 Strings with OR operation
            grep 'nike\|adidas' BrandData.csv > NikeORAdidasData.csv

          Example : Search For 2 Strings with AND operation  
           grep nike BrandData.csv  | grep shoe > NikeANDShoeData.csv 
 
      4.  Changing file or directory (folder) permissions.
        Sometimes it required to change the permissions of an file or directory in order to perform certain operations.
            Syntax : chmod permissionOctalCode fileOrDirectoryName
            Example : chmod 777 data
            chmod 777 Record.csv
            Note : Use -R option to recursively change the permission of the directory.
           
      5.  Searching file by name.
            find command is very useful to search files.
            Syntax : find directoryToSearch -name fileName
            Example : find / -name 'eaccmd.sh'

      6.  Access environment as different user.
            Using below command you can access the environment as an different user.Mostly it is used to access the environment as more privileged user(root). 
            Syntax : sudo su - userName
            Example : sudo su - admin

      7.  Navigate to previous directory.
             Using below command you can navigate to previous directory from where you have navigated to current directory.
             Syntax : cd - 

      8. Create a zip file.
            Using below command you can add single or multiple files to zip.
            Syntax : zip nameOfZip.zip file1 file2 ...
            Example : zip Log.zip server.log console.log

      9. Unzip the zipped file.
           Use below command to unzip the zipped file.
           Syntax : unzip nameOfZip.zip
           Example : unzip Log.zip
         
    10.  Display content of zip file without extracting it.
           You can display the content of zip file without extracting it.
           Syntax : unzip -l nameOfZip.zip

           Example : unzip -l Log.zip

     11.  Delete all lines containing specific text from a file.Without opening the file.
           You can delete all lines containing 'some string' from a file without opening it.
           Syntax : sed 
--in-place '/some string/d' myfile
           Example : sed --in-place '/nike/d' Brand.csv

For more detail on any of the above commands use man command(manual pages or man pages).

No comments:

Post a Comment