LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-01-2006, 05:08 PM   #1
beley
LQ Newbie
 
Registered: Sep 2003
Posts: 13

Rep: Reputation: 0
Very simple shell script - move files to different directories


I have a script that downloads image zips and extracts them. The images are in the format:

01234567_00.jpg
01234567_01.jpg
01234567_02.jpg
01234568_00.jpg

You get the idea. I need to move all the images to a directory by the last number before the underscore.

For instance,

01234567_00.jpg moves to the ln7 directory

01234563_00.jpg moves to the ln3 directory

Here is what I have so far to take the images and move them one at a time to their appropriate directories:

Code:
find *.jpg | while read FILE;
do
	dir=${FILE:7:1}
	# echo $FILE - $dir
	mv -f $FILE work/ln$dir/
	# echo Moved $FILE to ./work/ln$dir/$FILE
done
I get "Argument list too long" when I try to run the command. There are several thousand images in the directory.

Can I use xargs in a shell script like that?

I'm new to shell scripting - any help would be greatly appreciated.
 
Old 11-01-2006, 05:31 PM   #2
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,603

Rep: Reputation: 134Reputation: 134
Maybe protect your variables with ""'s ?
Code:
mv -f "$FILE" "work/ln$dir"
 
Old 11-01-2006, 05:33 PM   #3
beley
LQ Newbie
 
Registered: Sep 2003
Posts: 13

Original Poster
Rep: Reputation: 0
It's not the mv command that's getting the error on too many arguments... it's the find command.
 
Old 11-01-2006, 05:38 PM   #4
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,603

Rep: Reputation: 134Reputation: 134
Weird, the find statement works well here
Try to give the search path to find, eg if
it is in current dir :
Code:
find . -name "*.jpg" | while read FILE;
[edit]
I guess find *.jpg works because it is the bash shell
that extrapolates the *.jpg, not find

Last edited by keefaz; 11-01-2006 at 05:41 PM.
 
Old 11-01-2006, 06:37 PM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
please try this one

Code:
find -type f | grep jpg$ | \
while read FILE; do
	# DIR=In$(( ${${FILE/_*}/*\/} % 10 ))
	DIR=${FILE/_*}
	DIR=${DIR/*\/}
	DIR=$(( DIR % 10 ))
	DIR=In${DIR}

	mkdir -p "${DIR}"

	#mv -f "${FILE}" "${DIR}"
	mv "${FILE}" "${DIR}"
done
OR

Code:
IFS=$'\n'
for FILE in `find -type f | grep jpg$`; do
	# DIR=In$(( ${${FILE/_*}/*\/} % 10 ))
	DIR=${FILE/_*}
	DIR=${DIR/*\/}
	DIR=$(( DIR % 10 ))
	DIR=In${DIR}

	mkdir -p "${DIR}"

	#mv -f "${FILE}" "${DIR}"
	mv "${FILE}" "${DIR}"
done
IFS=$' \t\n'

Last edited by konsolebox; 11-01-2006 at 06:38 PM.
 
Old 11-01-2006, 06:48 PM   #6
beley
LQ Newbie
 
Registered: Sep 2003
Posts: 13

Original Poster
Rep: Reputation: 0
Not sure about those, but I've been researching and finally found something that worked... I created a simple function:

Code:
function large_mv ()
{       while read line1; do
			if echo "$line1" | grep -q ".jpg"
			then
				{
				dir=${line1:7:1}
      				mv -f $line1 work/ln$dir/
				echo Moved $line1 to work/ln$dir/$line1
				}
			else
				{
				 echo $line1 is not an image - not moved.
				}
			fi
       done
}

ls | large_mv
It seems to be working well so far.
 
Old 11-01-2006, 07:18 PM   #7
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Here is a different approach taking advantage of the patterns of the filenames:
Code:
for dir in $(find ./ -maxdepth 1 -type f -iname "[[:digit:]]*_[[:digit:]][[:digit:]].jpg" -printf '%f\n' | \ 
  sed 's/\([[:digit:]]*\)_.*/\1/' | sort | uniq); do 
     if [ ! -d "$dir" ]; then mkdir "$dir"; fi
     mv "${dir}"_* "$dir"
done
The find command finds your picture files matching your example pattern. The sed command removes the starting "./" characters and the _##.jpg pattern from the filenames. This is then sorted and a uniq list of directories produced. The mv command uses a wildcard so you are using a mv command only once per directory.
 
Old 11-02-2006, 05:24 AM   #8
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,160

Rep: Reputation: 504Reputation: 504Reputation: 504Reputation: 504Reputation: 504Reputation: 504
The error message you get is from find. If you do:
find *.jpg
it tells you argument list too long, because it expands *.jpg and passes all those file names to the find command.

You have to use
find -name "*.jpg"
or
find -name \*.jpg

jlinkels
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need a simple shell script please overwritting files. stefaandk Programming 9 10-11-2006 07:24 AM
Does anyone know how to move files to a particular directory using shell scripting RowanB Programming 3 11-11-2004 01:46 PM
move mouse pointer from shell script malo_umoran Slackware 1 11-06-2004 08:31 AM
Move certain files into different directories facets Programming 6 05-28-2004 01:20 PM
simple question - can you move header and lib files from one box to another without a dewcansam Linux - Newbie 3 11-06-2001 05:35 PM


All times are GMT -5. The time now is 05:08 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration