Skip to content

Some UNIX Exercices

pscpu

 $ ./pscpu -h
NAME
        pscpu

SYNOPSIS
        pscpu

DESCRIPTION
        outputs the 5 first processes that consumes the most CPU time, in ascending order

Use case:

 $ ./pscpu
gmonard   2619  1.3  5.5 831300 219248 ?       Sl   09:36   0:23 /usr/local/lib/opera/opera -geometry 1280x800+0+0
root      2027  1.3  0.0 193460  3940 ?        Sl   09:35   0:24 /usr/lib/udisks/udisks-daemon
root      1234  2.0  1.5 169712 60116 tty7     Rs+  09:31   0:41 /usr/bin/X :0 vt7 -br -nolisten tcp -auth /var/run/xauth/A:0-boRdJb
gmonard   2216  2.3  2.2 858076 89192 ?        Sl   09:35   0:40 kwin -session 106765640000135841344900000022410000_1366154194_510451
gmonard   3706 72.4  0.0 1686316  724 pts/4    D+   10:04   0:03 /opt/g09.B01-pgi64/g09/l716.exe 0 /home/gmonard/tmp/Gau-3706.chk 0 /home/gmonard/tmp/Gau-3706.int 0 /home/gmonard/tmp/Gau-3706.rwf 0 /home/gmonard/tmp/Gau-3706.d2e 0 /home/gmonard/tmp/Gau-3706.scr 0 /home/gmonard/tmp/Gau-3703.inp 0 junk.out 0 /home/gmonard/tmp/Gau-3706.nex 0

Commands to include in your bash script: ps, sort, tail (or head)

swapfile

NAME
        swapfiles

SYNOPSIS
        swapfiles FILE1 FILE2

DESCRIPTION
        swap filenames (FILE1 is renamed FILE2 and FILE2 is renamed FILE1)

Use case:

 $ ls
file1  file2
 $ cat file1
marry
had
a little
lamb
 $ cat file2
computational
chemistry
rocks!
 $ swapfiles file1 file2
 $ cat file1
computational
chemistry
rocks!
 $ cat file2
marry
had
a little
lamb

Commands to include in your bash script: mv, mktemp

motif

NAME
        motif

SYNOPSIS
        motif PATTERN

DESCRIPTION
        search for PATTERN in all files in the current directory whose suffix ends by .f, .F, .c, or .h
        Because Fortran files are case independent, the motif program search for patterns that are
        either lower case or upper case

Use case:

 $ motif implicit
amber2car.f:      implicit none 
checkinterdist.f:      implicit none
find_closest2.f:      implicit none
find_closest2.f:      implicit none
find_closest2.f:      implicit none
find_closest2.f:      implicit none
getmoving.f:      IMPLICIT NONE
mdcrdcenter-2.f:      implicit none
mdcrdcenter-2.f:      implicit none
mdcrdcenter-2.f:      implicit none
mdcrdcenter.f:      implicit none
mdcrdcenter.f:      implicit none
mdcrdcenter.f:      implicit none

Commands to include in your script: grep

motifl

NAME
        motifl

SYNOPSIS
        motifl PATTERN

DESCRIPTION
        output filenames whose suffix ends by .f, .F, .c, or .h in the current directory 
        and which contain PATTERN

Use case:

 $ ~/Ecrit/Conferences/2013/Ecole-CORREL-Paris/Unix/motifl implicit
amber2car.f
checkinterdist.f
find_closest2.f
getmoving.f
mdcrdcenter-2.f
mdcrdcenter.f

Commands to include in your script: grep

execall

NAME
        execall

SYNOPSIS
        execall COMMAND FILE1 [FILE2 [...]]

DESCRIPTION
        execute COMMAND for all files listing as arguments (FILE1, ...)

Use case:

 $ execall ls file1 file2
file1
file2
 $ execall cat file1 file2
computational
chemistry
rocks!
marry
had
a little
lamb
 $ execall "ls -l" file1 file2
-rw-r--r-- 1 gmonard users 31 Apr 17 09:39 file1
-rw-r--r-- 1 gmonard users 24 Apr 17 09:39 file2

Commands to include in your bash script: shift, bash -c

mmv

NAME
        mmv

SYNOPSIS
        mmv PATTERN1 PATTERN2

DESCRIPTION
        rename all file of type PATTERN1.* to PATTERN2.* files

        For example:
          mmv File1 File2
        will rename File1.a into File2.a, File1.b into File2.b, etc.

Use case:

 $ touch toto1.c toto1.o toto1.exe
 $ ls -l toto*
-rw-r--r-- 1 gmonard users 0 Apr 17 09:55 toto1.c
-rw-r--r-- 1 gmonard users 0 Apr 17 09:55 toto1.exe
-rw-r--r-- 1 gmonard users 0 Apr 17 09:55 toto1.o
 $ ../mmv toto1 toto2
 $ ls -l toto*
-rw-r--r-- 1 gmonard users 0 Apr 17 09:55 toto2.c
-rw-r--r-- 1 gmonard users 0 Apr 17 09:55 toto2.exe
-rw-r--r-- 1 gmonard users 0 Apr 17 09:55 toto2.o

cmprm

NAME
        cmprm

SYNOPSIS
        cmprm FILE1 FILE2

DESCRIPTION
        compare FILE1 and FILE2, delete FILE1 if both files are identical

Use case:

 $ ls -l file*
-rw-r--r-- 1 gmonard users 31 Apr 17 09:39 file1
-rw-r--r-- 1 gmonard users 24 Apr 17 09:39 file2
-rw-r--r-- 1 gmonard users 31 Apr 17 11:15 file3
 $ cat file1
computational
chemistry
rocks!
 $ cat file2
marry
had
a little
lamb
 $ cat file3
computational
chemistry
rocks!
 $ ../cmprm file1 file2
file1 identical to file2
 $ ls -l file*
-rw-r--r-- 1 gmonard users 31 Apr 17 09:39 file1
-rw-r--r-- 1 gmonard users 24 Apr 17 09:39 file2
-rw-r--r-- 1 gmonard users 31 Apr 17 11:15 file3
 $ ../cmprm file1 file3
remove file file1
 $ ls -l file*
-rw-r--r-- 1 gmonard users 24 Apr 17 09:39 file2
-rw-r--r-- 1 gmonard users 31 Apr 17 11:15 file3

Commands to include in your script: cmp, rm

comparerm

NAME
        comparerm

SYNOPSIS
        comparerm REMOTE_DIR

DESCRIPTION
        recursively compares the current directory with REMOTE_DIR directory,
        and delete all files in the current directory that are identical to a 
        file in the remote directory

directory bookmarking

NAME
       addcd, lscd, rmcd - bookmark directories

SYNOPSIS
        addcd BOOKMARK
        lscd
        rmcd BOOKMARK
        . BOOKMARK

DESCRIPTION
        addcd BOOKMARK - save the current directory name as the BOOKMARK tag

        lscd - list all the BOOKMARK tags (in directory order)

        rmcd BOOKMARK - remove the BOOKMARK tag in the list of bookmarks

        . BOOKMARK - change current directory to the directory represented by BOOKMARK

my own trash

NAME
       myrm, myls, myback, myempty - work your own trash

SYNOPSIS
        myls [OPTIONS]
        myrm FILE [FILE [...]]
        myback FILE [FILE [...]]
        myempty

DESCRIPTION
        myls list the content of the trash

        myrm put one or more FILE(S) to the trash

        myback retrieve one or more FILE(S) from the trash directory to the current directory

        myempty empties the trash directory