Some Useful Linux or Unix Commands

Simple Commands

Bash Scripts

Good Stuff to have in your .bashrc

  • case $(id -u) in
        0)
            STARTCOLOUR='\[\e[31m\]';
            ;;
        *)
            STARTCOLOUR='\[\e[36m\]';
            ;;
    esac
    ENDCOLOR="\[\e[0m\]"
    PS1="$STARTCOLOUR\u@\h:\w$ $ENDCOLOR";

    export LS_OPTIONS='--color=auto -h'
    alias ls='ls $LS_OPTIONS'

    export FIGNORE=.svn

    alias sls='screen -ls'
    alias sn='screen -S'
    alias sr='screen -r'

Delete all files in a directory, except the first 100 files

  • #!/bin/bash
    total=`ls $1 | wc -l`
    deleteTail=`expr $total - 100`
    rm -v `find $1 | tail -n $deleteTail`
  • call it after copying it to a file keepOnlyFirst100.sh by: ./keepOnly100.sh directoryName

Call a script with all directories in your current directory

  • find . -maxdepth 1 -type d -exec ./keepOnlyFirst100.sh '{}' \;

Apply a script which has multiple inputs to all txt files in a folder

  • The 3 inputs here are a number and the input file and output file:
  • find *.txt -maxdepth 1 -exec ./avgimg '400' '{}' '{}.jpg' \;

Append a line to all files in a folder

Other

Perl

How to install new CPAN modules?

  • perl -MCPAN -e shell # go to CPAN install mode
    install Bundle::CPAN # update CPAN
    reload cpan
    install Set::Scalar

Perl One-Liners

Sending multiple commands to screen session on different machines

  • #!/usr/bin/perl -w

    # This script creates screen sessions, ssh's to machines and executes code on these machines.
    # parameters: -s (start) -r (run) -q (quit)
    # HowTo:
    #  1) change the executed code, property folder and prefix to your values
    #  2) select your machines
    #  3) on the machine where you want your screen sessions run to start your sessions: ./clusterSubmitJobs.pl -s
    #  4) once you're done and want to quit all your sessions: ./clusterSubmitJobs.pl -q
    # author: richard socher.org

    use strict;
    use Getopt::Std;
    use List::Util qw[min max];
    my %options=();
    getopts("srq",\%options);

    #------------------
    # files to be considered
    my $folder = '/folderWithInputFiles';
    my $prefix = 'tests_';
    my $ext = '.config';
    # code to run with files
    my $code = './runMyScript.sh -configFile ';

    # deprecated by mstat
    my @freemachines = ('machine1.yourPlace.edu', 'machine2.yourPlace.edu');
    #-------------------


    my $full = $folder . $prefix . '*' . $ext;
    print "Using files: $full \n";

    my @files = <$full*>;
    my $numMachines = @freemachines;
    my $numFiles = @files;
    my $minNum = min($numMachines,$numFiles);

    for (my $i = 0; $i < $minNum; $i++) {
      if ($options{s}){
        print "Creating screen session: freemachines[$i] for \t $files[$i] \n";
        system("screen -d -m -S $freemachines[$i]");
        system("screen -S $freemachines[$i] -p 0 -X stuff \"ssh $freemachines[$i]\015\"");
      }

      if ($options{r}){
        print "run: screen -S $freemachines[$i] -p 0 -X stuff \"$code $files[$i]\"\n";
        system("screen -S $freemachines[$i] -p 0 -X stuff \"$code $files[$i]\015\"");
      }

      if ($options{q}){
        print "screen -S $freemachines[$i] -p 0 -X stuff \"exit\n";
        system("screen -S $freemachines[$i] -p 0 -X stuff \"exit\015\"");
        system("screen -S $freemachines[$i] -p 0 -X stuff \"exit\015\"");
      }
    }

Extracting multiple multiline patterns between a start and an end tag using regular expressions

  • Here, we want to extract everything between <parse> and </parse>.
  • #!/usr/bin/perl -w

    local $/;

    open(DAT, "yourFile.xml") || die("Could not open file!");
    my $content = <DAT>;

    while ($content =~ m/<parse>(.*?)<\/parse>/sg){
     print "$1\n"
    };
Add Comment 
Sign as Author 
Enter code: