Some Useful Linux or Unix Commands

Simple Commands

Bash Scripts

For loop over a bunch of directories and run some command in each:

  • submitFolder=../gw/submit/
    resultsFile=allSentencesTeams.txt
    rm $resultsFile
    for dir in $submitFolder*/
    do
    dir=${dir%*/}
    java -jar pcfg.jar generate -n 20 $submitFolder${dir##*/}/S1.gr $submitFolder${dir##*/}/Vocab.gr >> $resultsFile
    done

Find a string in the last n (9) updated files in the current directory (and print out only that string)

  • ls -rt | tail -n9 | xargs -l grep results $1 | grep  -Eo " 91..."

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'

    # make sure bash updates it's (echo $COLUMNS) when the size changes
    # so there are no ugly overlay effects of long lines
    shopt -s checkwinsize
     

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 '{}' \;

Bash One-Liner: 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' \;

Bash One-Liner: Rename all files ending in *final to *final.mat

  • for i in *final; do mv "$i" "$i.mat"; done

Bash One-Liner: Delete all directories that are older than a certain time

  • Here, we delete all folders that are older than 19 minutes and include the string _qsub
  • find . -type d -mmin +19 | grep _qsub | xargs rm -rf

Bash One-Liner: Append a line to all files in a folder

Bash One-Liner: Delete a several qsub'ed jobs:

Bash One-Liner: Delete all .class files from git repo

Password-less ssh key

Quick guide for setting up an svn

Perl

Comments

Add Comment 
Sign as Author 
Enter code: