Any decent shell scripters in the house?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HorseShit
    Too lazy to set a custom title
    • Dec 2004
    • 17513

    #1

    Any decent shell scripters in the house?

    I have a directory 'x' that I need to to move all .jpg's from and put them into another folder. The directory 'x' has many other folders inside of it so this will need to be a recursive string. Who can help? I'll throw you a bone via paypal or something if needed.
  • HorseShit
    Too lazy to set a custom title
    • Dec 2004
    • 17513

    #2
    btw this is for freebsd/bash

    Comment

    • HorseShit
      Too lazy to set a custom title
      • Dec 2004
      • 17513

      #3
      come on peoples.. wheres fuzebox/rob

      Comment

      • ScriptWorkz
        Confirmed User
        • Jul 2007
        • 274

        #4
        if you can run a php script to do it i could help you out (i generally use php instead of sh for most shell scripts anyway, so i'm pretty rusty on actual sh scripting)

        Comment

        • videodoll
          Confirmed User
          • Sep 2006
          • 196

          #5
          I think that is very simple to do without a script unless i don't understand what you want.

          www.nikki9teen.com
          www.amandaedge.com

          ICQ: 415397976

          Comment

          • HorseShit
            Too lazy to set a custom title
            • Dec 2004
            • 17513

            #6
            I probably could use a php script

            Comment

            • HorseShit
              Too lazy to set a custom title
              • Dec 2004
              • 17513

              #7
              directory 'x' has many sub directories within it each with multiple .jpg's

              I basically want to move (or copy if needed) all .jpg's from within these sub directories into one directory 'y' so that 'y' contains all .jpg's from 'x' and it's sub directories.

              Comment

              • ScriptWorkz
                Confirmed User
                • Jul 2007
                • 274

                #8
                just tried to hit you on icq, i can help you out, just have a few quick questions

                Comment

                • scriptdog
                  Registered User
                  • May 2008
                  • 3

                  #9
                  hello, i can do that, just explain me..

                  you have a tree where you want to take all jpgs out and put it where?

                  i just need that you can run a php, if you have a webserver in that machine is very easy to do, if not we just have to download a php and unzip it in any folder

                  Comment

                  • mikesouth
                    Confirmed User
                    • Jun 2003
                    • 6334

                    #10
                    this is gonna be close

                    find (top level directory) / -name *.jpg -print | mv (new directory)

                    that should start you that may recreate the structure of the original dir ( i dont remember for sure)

                    if so you can put it in a for x in loop something like this

                    for x in `ls -1 -r (top level directory)/*.jpg`
                    do
                    mv x (newdirectory)/.
                    done
                    Mike South

                    It's No wonder I took up drugs and alcohol, it's the only way I could dumb myself down enough to cope with the morons in this biz.

                    Comment

                    • ScriptWorkz
                      Confirmed User
                      • Jul 2007
                      • 274

                      #11
                      do you want jpg's in subdirectories of x to show up in corresponding subdirectories in y or just in the top level y folder?

                      Comment

                      • HorseShit
                        Too lazy to set a custom title
                        • Dec 2004
                        • 17513

                        #12
                        I don't want it to create any sub directories in 'y' just dump ALL jpg's from 'x' and recursive directories

                        Comment

                        • mikesouth
                          Confirmed User
                          • Jun 2003
                          • 6334

                          #13
                          Originally posted by jdavis
                          I don't want it to create any sub directories in 'y' just dump ALL jpg's from 'x' and recursive directories
                          this'll do it

                          for x in `ls -1 -r (top level directory)/*.jpg`
                          do
                          mv $x (newdirectory)/.
                          done
                          Last edited by mikesouth; 05-15-2008, 07:45 AM.
                          Mike South

                          It's No wonder I took up drugs and alcohol, it's the only way I could dumb myself down enough to cope with the morons in this biz.

                          Comment

                          • ScriptWorkz
                            Confirmed User
                            • Jul 2007
                            • 274

                            #14
                            PHP Code:
                            #!/usr/bin/php
                            <?
                            // set $base_dir to the source dir, and $dest_dir to where you want files moved too, include trailing /
                            $base_dir = "/source/dir/";
                            $dest_dir = "/dest/dir/";
                            
                            // function for recursion
                            function process_dir($dir) {
                                global $dest_dir;
                                
                                $d = opendir($dir);
                                
                                while (($f = readdir($d)) !== FALSE) {
                                    if (strcmp($f, ".") && strcmp($f, "..")) {
                                        if (is_dir($dir . $f)) {
                                            process_dir($dir . $f);
                                        } else if (is_file($dir . $f)) {
                                            $tmp = explode(".", $f);
                                            $ext = strtolower($tmp[(count($tmp) - 1)]);
                                        
                                            if (!strcmp($ext, "jpg") || !strcmp($ext, "jpeg")) {
                                                // to copy files
                                                copy($dir . $f, $dest_dir . $f);
                                            
                                                // uncomment to move files instead, comment above
                                                // rename($dir . $f, $dest_dir . $f)
                                            }
                                        }
                                    }
                                }
                                
                                closedir($d);
                            }
                            
                            process_dir($base_dir);
                            ?>
                            i didn't test it, but if you save that as say move-pics.php and chmod it +x then ./move-pics.php it should do the trick. if you want files moved instead of copied, look at the comments and comment the copy line out and remove the commenting on the rename line. you also need to make sure the shebang at the top points to your php binary and the $base_dir and $dest_dir variables are set to your x and y directories respectively.

                            EDIT: just saw mike souths post, as a shell script that'd probably work a little easier then mine, atleast you have a backup.
                            Last edited by ScriptWorkz; 05-15-2008, 07:47 AM.

                            Comment

                            • HorseShit
                              Too lazy to set a custom title
                              • Dec 2004
                              • 17513

                              #15
                              thanks for the suggestions. what about this, i realize it's for copying and not moving

                              find $SRC -iname '*.jpg' -exec cp '{}' ${DEST}/ \;

                              Comment

                              • mikesouth
                                Confirmed User
                                • Jun 2003
                                • 6334

                                #16
                                yes thats for copying not moving...I gave you a for...do loop that will do what you want

                                its a shell script
                                Mike South

                                It's No wonder I took up drugs and alcohol, it's the only way I could dumb myself down enough to cope with the morons in this biz.

                                Comment

                                • scriptdog
                                  Registered User
                                  • May 2008
                                  • 3

                                  #17
                                  god bless linux

                                  Comment

                                  • HorseShit
                                    Too lazy to set a custom title
                                    • Dec 2004
                                    • 17513

                                    #18
                                    I would prefer to have it not overwrite files with the same name but that gets more complicated

                                    Comment

                                    • mikesouth
                                      Confirmed User
                                      • Jun 2003
                                      • 6334

                                      #19
                                      Originally posted by jdavis
                                      I would prefer to have it not overwrite files with the same name but that gets more complicated
                                      not really just add a counter variable in the loop and append it to the filename
                                      Mike South

                                      It's No wonder I took up drugs and alcohol, it's the only way I could dumb myself down enough to cope with the morons in this biz.

                                      Comment

                                      • HorseShit
                                        Too lazy to set a custom title
                                        • Dec 2004
                                        • 17513

                                        #20
                                        Originally posted by mikesouth
                                        not really just add a counter variable in the loop and append it to the filename
                                        do it up

                                        Comment

                                        • mikesouth
                                          Confirmed User
                                          • Jun 2003
                                          • 6334

                                          #21
                                          my shell scripting is a tad rusty and I dont have man pages here but
                                          a=0
                                          for x in `ls -1 -r (top level directory)/*.jpg`
                                          do
                                          a++
                                          mv $x (newdirectory)/$x$a.jpg
                                          done

                                          will be on the right track though you may have to snip out the first .jpg with a substr command if that gives you .jpg twice (and I think it will) Im flying by the seat of my pants here but you get the gist yer on a UNIX system (or derivative) use man pages for substr for the syntax

                                          man substr
                                          Mike South

                                          It's No wonder I took up drugs and alcohol, it's the only way I could dumb myself down enough to cope with the morons in this biz.

                                          Comment

                                          • Socks
                                            Confirmed User
                                            • May 2002
                                            • 8475

                                            #22
                                            http://www.linuxquestions.org/questi...-files-199134/

                                            this might work:

                                            for i in `find <directory> -type d`; do cp *.jpg $i; done

                                            Comment

                                            • HorseShit
                                              Too lazy to set a custom title
                                              • Dec 2004
                                              • 17513

                                              #23
                                              OK people $50 to the first person that gives me this script. I think mikesouth is closest to what I'm looking for but it needs to be a script that doesn't need me hacking it to get it working. I would prefer not to have a php script, just a plain old bash one like Mike is proposing. It must not rewrite other .jpgs with the same name but instead rename the file names incrementally if it spots duplicates.

                                              Comment

                                              • HorseShit
                                                Too lazy to set a custom title
                                                • Dec 2004
                                                • 17513

                                                #24
                                                BTW, I am not available on ICQ today..

                                                Comment

                                                • HorseShit
                                                  Too lazy to set a custom title
                                                  • Dec 2004
                                                  • 17513

                                                  #25
                                                  bump to give someone money

                                                  Comment

                                                  • HorseShit
                                                    Too lazy to set a custom title
                                                    • Dec 2004
                                                    • 17513

                                                    #26
                                                    bump, would like to have this script in my hand today

                                                    Comment

                                                    • scoremoola
                                                      Registered User
                                                      • May 2008
                                                      • 79

                                                      #27
                                                      save the code below into a file call it whatever you like and run it like so
                                                      python nameofscript.py foldertoprocess
                                                      -------------------------------

                                                      PHP Code:
                                                      import os, os.path, sys
                                                      
                                                      destdir = "/destdir"
                                                      
                                                      os.system( "mkdir " + destdir )
                                                      
                                                      for root, dirs, files in os.walk( sys.argv[1]  ):
                                                          for name in files:
                                                                  name = os.path.join(root, name)
                                                                  command = "mv \"" + name + "\" \"" + destdir + "\""
                                                                  os.system( command ) 
                                                      

                                                      www.GetSCORECash.com
                                                      lando | icq: 337-369-506
                                                      I wasn't born as much as I fell out

                                                      Comment

                                                      • HorseShit
                                                        Too lazy to set a custom title
                                                        • Dec 2004
                                                        • 17513

                                                        #28
                                                        Have you tested it out? Will it dump all .jpg files into ONE directory 'y' without overwriting existing files with the same name?

                                                        Comment

                                                        • mrkris
                                                          Confirmed User
                                                          • May 2005
                                                          • 2737

                                                          #29
                                                          Originally posted by scoremoola
                                                          save the code below into a file call it whatever you like and run it like so
                                                          python nameofscript.py foldertoprocess
                                                          -------------------------------

                                                          PHP Code:
                                                          import os, os.path, sys
                                                          
                                                          destdir = "/destdir"
                                                          
                                                          os.system( "mkdir " + destdir )
                                                          
                                                          for root, dirs, files in os.walk( sys.argv[1]  ):
                                                              for name in files:
                                                                      name = os.path.join(root, name)
                                                                      command = "mv \"" + name + "\" \"" + destdir + "\""
                                                                      os.system( command ) 
                                                          
                                                          Holy .... abort.

                                                          PHP-MySQL-Rails | ICQ: 342500546

                                                          Comment

                                                          • Socks
                                                            Confirmed User
                                                            • May 2002
                                                            • 8475

                                                            #30
                                                            Originally posted by Socks
                                                            http://www.linuxquestions.org/questi...-files-199134/

                                                            this might work:

                                                            for i in `find <directory> -type d`; do cp *.jpg $i; done
                                                            Pretty sure this does what you want in Bash, try it out

                                                            Comment

                                                            • GrouchyAdmin
                                                              Now choke yourself!
                                                              • Apr 2006
                                                              • 12085

                                                              #31
                                                              Jesus tapdancing Christ.
                                                              Code:
                                                              #!/bin/bash
                                                              TWIRLY1="|"
                                                              TWIRLY2="/"
                                                              TWIRLY3="-"
                                                              TWIRLY4="\"
                                                              DESTDIR=/tmp
                                                              WHEREFROM=/home
                                                              TWIRL=1
                                                              for n in `find $WHEREFROM -name \*.jpg -print`; do
                                                              TWIRL=$TWIRL+1
                                                              IF [ $TWIRL -gt 4 ]; then
                                                                TWIRL=1
                                                              fi
                                                              COUNT=0
                                                              if [ -f $DESTDIR/$n ]; then
                                                                # Ho ho ho.
                                                                $newname = $RAND`basename $n`;
                                                              else
                                                                $newname = `basename $n`;
                                                              fi
                                                              echo -n "Imma busy fuckkin up $newname, hold on..  $TWIRL"
                                                              mv $n $destdir/$newname
                                                              done
                                                              echo ""
                                                              echo "THIS SCRIPT IS SHAREWARE.  PLS REGISTER!"

                                                              Comment

                                                              • mrkris
                                                                Confirmed User
                                                                • May 2005
                                                                • 2737

                                                                #32
                                                                Originally posted by GrouchyAdmin
                                                                Jesus tapdancing Christ.
                                                                Code:
                                                                #!/bin/bash
                                                                TWIRLY1="|"
                                                                TWIRLY2="/"
                                                                TWIRLY3="-"
                                                                TWIRLY4="\"
                                                                DESTDIR=/tmp
                                                                WHEREFROM=/home
                                                                TWIRL=1
                                                                for n in `find $WHEREFROM -name \*.jpg -print`; do
                                                                TWIRL=$TWIRL+1
                                                                IF [ $TWIRL -gt 4 ]; then
                                                                  TWIRL=1
                                                                fi
                                                                COUNT=0
                                                                if [ -f $DESTDIR/$n ]; then
                                                                  # Ho ho ho.
                                                                  $newname = $RAND`basename $n`;
                                                                else
                                                                  $newname = `basename $n`;
                                                                fi
                                                                echo -n "Imma busy fuckkin up $newname, hold on..  $TWIRL"
                                                                mv $n $destdir/$newname
                                                                done
                                                                echo ""
                                                                echo "THIS SCRIPT IS SHAREWARE.  PLS REGISTER!"
                                                                I USE IT AND STAND BY ITS FUNCTIONALITY. REGISTERED!

                                                                PHP-MySQL-Rails | ICQ: 342500546

                                                                Comment

                                                                • scoremoola
                                                                  Registered User
                                                                  • May 2008
                                                                  • 79

                                                                  #33
                                                                  yes it will dump everything into one folder... those were the specs asked for

                                                                  www.GetSCORECash.com
                                                                  lando | icq: 337-369-506
                                                                  I wasn't born as much as I fell out

                                                                  Comment

                                                                  • HorseShit
                                                                    Too lazy to set a custom title
                                                                    • Dec 2004
                                                                    • 17513

                                                                    #34
                                                                    grouchyadmin, shoot me an email to area51 at gmail please

                                                                    Comment

                                                                    • GrouchyAdmin
                                                                      Now choke yourself!
                                                                      • Apr 2006
                                                                      • 12085

                                                                      #35
                                                                      Originally posted by jdavis
                                                                      grouchyadmin, shoot me an email to area51 at gmail please
                                                                      Hi Justin.

                                                                      I hit you up as requested.

                                                                      Comment

                                                                      • GrouchyAdmin
                                                                        Now choke yourself!
                                                                        • Apr 2006
                                                                        • 12085

                                                                        #36
                                                                        Oh, incidentally, this should actually do what you want without being quite so obnoxious. I don't trust having a 'smart' shell; just basic utilities that have been in UNIX since the 70s.

                                                                        Code:
                                                                        #!/bin/sh
                                                                        PATH=/bin:/usr/bin:/sbin:/usr/sbin
                                                                        FROMDIR=SET_BASEDIR_FOR_MOVE_FROM_HERE
                                                                        TODIR=SET_DIR_TO_COPY_IT_TO_HERE
                                                                        FILETYPE=jpg
                                                                        APPENDSTART=1
                                                                        for n in `find $FROMDIR -name \*.$FILETYPE -print`; do
                                                                           FILE=`basename "$n"`
                                                                           if [ -f "$TODIR/$FILE" ]; then
                                                                             while [ $APPENDSTART -le 100 ]; do
                                                                               APPENDSTART=`expr $APPENDSTART + 1`
                                                                               if [ ! -f "$TODIR/$APPENDSTART$FILE" ]; then
                                                                                 FILE="$APPENDSTART$FILE"
                                                                                 break;
                                                                               fi
                                                                             done
                                                                           fi
                                                                           echo "Moving '$n' to '$TODIR/$FILE'."
                                                                           mv "$n" "$TODIR/$FILE"
                                                                        done

                                                                        Comment

                                                                        Working...