[Dclug] useful bash shell function

Bruce Israel israel at tux.org
Mon Oct 4 18:38:23 EDT 2010


*chuckle*; someone today asked me for my equivalent one, which I call
'storefiles'; it's usage is 'storefiles [ -cp] <dir> <files>'.  I
invert the argument order from 'mv' which is the way you do it because
it makes it much easier both to parse the arguments and to work with
it (for example, you can do something like 

    alias do-backup='storefiles backup.$(date %Y.%m.%d)'

and then call it like 'do-backup *.java *.txt').

One thing though; It seems a bit too complicated.  I run mine on
Linux, on Windows (under cygwin), and on Mac OS X, and I don't need to
conditionalize anything on a per-OS basis.  Why did you need to do
that?

This is a good idea;  I'll pull out some of my useful commands,
scripts, tricks, etc. and post them.

To start things off, here is my command 'mark'.

# mark the current dir or optional specified file with a shell variable
mark() {
    prf="${PWD}/"
    if echo $2 | grep -q '^/'; then prf=""; fi
    eval "${1}='${prf}${2}'" ;
    echo "${1} ==> ${prf}${2}" ; 
}

'mark' marks a file or directory with a shell variable.  You can use
it for picking up and moving files easily; e.g.

    cd /tmp/src/project
    mark mycode MyGreatNewProgram.java
    cd /home/israel/sources
    mv $mycode .

or marking directories, like:

    cd /home/israel/project/path/with/a/very/long/name
    mark thisdir
    cd
    cp <files> $thisdir
    cd $thisdir

Enjoy.

Bruce





On Mon, Oct 04, 2010 at 05:29:06PM -0400, Timothy Ball wrote:
> yet another one of my hacks but this one is particularly useful. 
> 
> put this into a file and then source that file in your ~/.bashrc or
> ~/.profile. i have /bin/bash at the top but this should work for any ksh
> compliant shell as long as you have seq(1) (which you really should).
> 
> this code is also available at
> http://www.tux.org/~timball/shell_snippets/mkmv.sh
> 
> use is something like:
> $ source mkmv.sh
> $ mkmv file1 file2 file3 ... filen dest_dir
> 
> --timball
> 
> --snip--snip--snip--
> #!/bin/bash
> #
> # function to move files to create a dir then move files into it
> #
> # This is free software. 
> #    - ask timball about the license. most likely GPL v2
> #    - There is no guarantee of anything.
> #
> #            --timball at sunlighfoundation.com
> #            Mon Oct  4 17:14:14 EDT 2010
> #
> # $Id$
> #
> 
> function mkmv {
> # This is an idea who's time has come. a way to both make and move stuff into a dir... genius!
>     declare -a list_a
> 
>     # ghetto OS detection 
>     if [[ $OSTYPE == darwin* ]]; then 
>         # echo "i'm a darwin"
>         SEQ=gseq
>     elif [[ $OSTYPE == linux* ]]; then 
>         # echo "i'm a linux"
>         SEQ=seq
>     else 
>         echo "i'm an other OS"
>         SEQ=seq
>     fi
> 
>     eval lastone=\$$#
> 
>     # see if we can at least make the dir
>     mkdir -p $lastone
>     if [ "$?" -ne 0 ]
>     then
>         echo "unable to create dir, bailing"
>         return $?
>     fi
> 
>     # build array of things to move
>     for i in $( $SEQ '1' $(( $# - 1)) )
>     do
>         eval arg=\$$i
>         #echo -n "$arg "
>         list_a[$(( $i - 1 ))]=$arg
>     done
> 
>     # move stuff
>     mv ${list_a[*]} $lastone
>     if [ "$?" -ne 0 ]
>     then
>         echo "unable to move stuff, bailing. Please do sanity checking."
>         return $?
>     fi
> }
> --snip--snip--snip--
> 
> 
> -- 
> 	GPG key available on pgpkeys.mit.edu
> pub  1024D/511FBD54 2001-07-23 Timothy Lu Hu Ball <timball at tux.org>
> Key fingerprint = B579 29B0 F6C8 C7AA 3840  E053 FE02 BB97 511F BD54
> _______________________________________________
> Dclug mailing list
> Dclug at calypso.tux.org
> http://calypso.tux.org/mailman/listinfo/dclug


More information about the Dclug mailing list