GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Any decent shell scripters in the house? (https://gfy.com/showthread.php?t=828628)

HorseShit 05-15-2008 08:01 AM

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 05-15-2008 08:04 AM

btw this is for freebsd/bash

HorseShit 05-15-2008 08:13 AM

come on peoples.. wheres fuzebox/rob

ScriptWorkz 05-15-2008 08:28 AM

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)

videodoll 05-15-2008 08:31 AM

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

HorseShit 05-15-2008 08:31 AM

I probably could use a php script

HorseShit 05-15-2008 08:34 AM

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.

ScriptWorkz 05-15-2008 08:34 AM

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

scriptdog 05-15-2008 08:35 AM

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

mikesouth 05-15-2008 08:37 AM

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

ScriptWorkz 05-15-2008 08:38 AM

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?

HorseShit 05-15-2008 08:40 AM

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

mikesouth 05-15-2008 08:44 AM

Quote:

Originally Posted by jdavis (Post 14193378)
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

ScriptWorkz 05-15-2008 08:45 AM

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.

HorseShit 05-15-2008 08:50 AM

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

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

mikesouth 05-15-2008 08:52 AM

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

its a shell script

scriptdog 05-15-2008 08:52 AM

god bless linux

HorseShit 05-15-2008 08:53 AM

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

mikesouth 05-15-2008 08:56 AM

Quote:

Originally Posted by jdavis (Post 14193418)
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

HorseShit 05-15-2008 09:00 AM

Quote:

Originally Posted by mikesouth (Post 14193441)
not really just add a counter variable in the loop and append it to the filename

do it up :thumbsup

mikesouth 05-15-2008 09:47 AM

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

Socks 05-15-2008 09:54 AM

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

this might work:

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

HorseShit 05-16-2008 05:35 AM

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.

HorseShit 05-16-2008 05:40 AM

BTW, I am not available on ICQ today..

HorseShit 05-16-2008 07:09 AM

bump to give someone money

HorseShit 05-16-2008 08:11 AM

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

scoremoola 05-16-2008 08:28 AM

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

PHP Code:

import osos.pathsys

destdir 
"/destdir"

os.system"mkdir " destdir )

for 
rootdirsfiles in os.walksys.argv[1]  ):
    for 
name in files:
            
name os.path.join(rootname)
            
command "mv \"" name "\" \"" destdir "\""
            
os.systemcommand 


HorseShit 05-16-2008 08:39 AM

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

mrkris 05-16-2008 08:39 AM

Quote:

Originally Posted by scoremoola (Post 14197665)
save the code below into a file call it whatever you like and run it like so
python nameofscript.py foldertoprocess
-------------------------------

PHP Code:

import osos.pathsys

destdir 
"/destdir"

os.system"mkdir " destdir )

for 
rootdirsfiles in os.walksys.argv[1]  ):
    for 
name in files:
            
name os.path.join(rootname)
            
command "mv \"" name "\" \"" destdir "\""
            
os.systemcommand 


Holy .... abort.

Socks 05-16-2008 08:43 AM

Quote:

Originally Posted by Socks (Post 14193786)
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

GrouchyAdmin 05-16-2008 08:45 AM

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!"


mrkris 05-16-2008 08:54 AM

Quote:

Originally Posted by GrouchyAdmin (Post 14197713)
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!

scoremoola 05-16-2008 08:59 AM

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

HorseShit 05-16-2008 09:08 AM

grouchyadmin, shoot me an email to area51 at gmail please

GrouchyAdmin 05-16-2008 09:47 AM

Quote:

Originally Posted by jdavis (Post 14197792)
grouchyadmin, shoot me an email to area51 at gmail please

Hi Justin.

I hit you up as requested.

GrouchyAdmin 05-16-2008 09:51 AM

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



All times are GMT -7. The time now is 07:17 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123