Most frequently used Linux Commands

There are hundreds of commands in Linux. But we use only few of them on regular basis. Let us see most frequently used commands.
  • ls - to list the files
  • mkdir and cd - to make a new directory and traverse to directories
  • cp, mv, rm - to copy. move and delete files and directories
  • cat - to show file contents
  • chmod and chown - to change the permissions and owner of the files and directories
  • pwd - to see present working directory
  • ps - to get process information
  • df and du - to get free disk space and disk usage statistics
  • kill - to kill the process
  • head and tail - to see the file contents from top or bottom
  • ifconfig - to see the ip address and network card details
  • uname - to get system info
  • man, which, whereis - to get command syntax info and details
  • export - to set the environment variable
  • tar - to archive and extract .tar files
  • grep - to find the pattern in file or text
  • find - to find the files
  • grep - to find the pattern in file or text
  • ssh - to connect to remote host using SSH
  • vim - to create or edit text files
  • sed - to replace the matched pattern
  • awk - to process the records
  • crontab - to work with cron jobs. Comments can be added in cron file using # symbol. "errors in crontab file, can't install" this error means you have syntax error in cron expression. You can find the cron log - sudo grep "php" /var/log/cron
In automation testing, there are often situations where we need to execute the process and see the output after execution is finished. Below example shows how to execute the linux commands using Java. Note that we have executed piped command "ls | wc" using Runtime class.

Example


    @Test
    public void testProcess(){
        String output;
        Process p;
        
        try {
            String[] cmd = { "/bin/sh", "-c", "ls | wc" };
            p = Runtime.getRuntime().exec(cmd);
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            while ((output = br.readLine()) != null)
                System.out.println( output);
            p.waitFor();
            System.out.println ("exit code " + p.exitValue());
            p.destroy();
        } catch (Exception e) {}
    
    }

Before you run any shell script, you need to make sure that user has permissions to run the script. If permissions are not there, you need to modify the file permissions first and then you can execute the sript. To make the script executable by everyone, you can use below command.

chmod +x myscript.sh

To make the script executable by owner user, you can use below command.

chmod u+x myscript.sh

Then you can exeucte below command to run the script.

./myscript.sh

Web development and Automation testing

solutions delivered!!