LinuxQuestions.org
Review your favorite Linux distribution.
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 04-08-2011, 04:13 PM   #16
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983

Quote:
Originally Posted by rjo98 View Post
getting somewhere with ramurd's suggestion...

now i get

mkdir: invalid option -- i
What you're doing it's really dangerous... have you a backup of the Pictures folder? What if instead of a bash error (that causes the command to abort) it brings to an unpredictable result? Again, keep it simple...
 
Old 04-08-2011, 04:20 PM   #17
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by rjo98 View Post
getting somewhere with ramurd's suggestion...

now i get

mkdir: invalid option -- i
Given that:
1) none of the mkdirs stated by Nominal should give an -- i option
2) Nominal's commands are quoted thoroughly

I expect you're not putting all the quotes in there, they're very important, otherwise you can get bad and unpredictable results.
Otherwise Nominal's commands are safe and not dangerous at all; in fact very simple as it's all in one command that suits all.

To test out the commands you actually get, you can try to put an "echo" in front of them (so mkdir ... becomes echo mkdir ...) and see if you get results that are not so logical... Again, I expect a few missing quotes around {}.
 
Old 04-08-2011, 04:24 PM   #18
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
that's what i do get, the only i that i have after a - is for the cp, maybe something isn't copying and pasting right from the web to my CLI.
 
Old 04-08-2011, 04:27 PM   #19
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Ramurd View Post
I expect you're not putting all the quotes in there, they're very important, otherwise you can get bad and unpredictable results.
Otherwise Nominal's commands are safe and not dangerous at all; in fact very simple as it's all in one command that suits all.
Have you tested it? I tried the exact command suggested by Nominal Animal and then I applied your modification: I got the same results (errors) as the OP.

Last edited by colucix; 04-08-2011 at 04:29 PM.
 
Old 04-08-2011, 04:27 PM   #20
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
does anyone see what I did wrong?


find /Pictures/ -name '*A.jpg' -exec mkdir -p "/Resized/`dirname '{}'`" \&\& cp -vi '{}' "/Resized/`dirname '{}'`" ';'
 
Old 04-08-2011, 04:40 PM   #21
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
while read src
do
dst=${src/Pictures/Resized}
echo mkdir -p $(dirname $dst)
echo mv $src $dst
done < <(find /home/rjo98/Pictures -name \*a.jpg)


I'm trying to modify this, would i make it like this? I'm so confused right now. My pictures folder is /Pictures and i want them to go to /Resized if they are *A.jpg

while read src
do
dst=${src/Resized}
echo mkdir -p $(dirname $dst)
echo mv $src $dst
done < <(find /Pictures -name \*A.jpg)
 
Old 04-08-2011, 04:44 PM   #22
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by rjo98 View Post
does anyone see what I did wrong?


find /Pictures/ -name '*A.jpg' -exec mkdir -p "/Resized/`dirname '{}'`" \&\& cp -vi '{}' "/Resized/`dirname '{}'`" ';'
The argument '{}' is not correctly interpreted inside the command substitution. Look at the difference between the results of the two commands:
Code:
find /Pictures/ -name '*A.jpg' -exec dirname '{}' \;
and
Code:
find /Pictures/ -name '*A.jpg' -exec echo `dirname '{}'` \;
or even
Code:
find /Pictures/ -name '*A.jpg' -exec echo `ls -l '{}'` \;
which will result in a more explicit error.
 
Old 04-08-2011, 04:52 PM   #23
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by rjo98 View Post
I'm trying to modify this, would i make it like this? I'm so confused right now. My pictures folder is /Pictures and i want them to go to /Resized if they are *A.jpg

while read src
do
dst=${src/Resized}
echo mkdir -p $(dirname $dst)
echo mv $src $dst
done < <(find /Pictures -name \*A.jpg)
Nope. The statement
Code:
${src/Pictures/Resized}
is a parameter substitution to replace the string Pictures with the string Resized in the src variable. You have to leave it untouched. The following should work:
Code:
while read src
do
  dst=${src/Pictures/Resized}
  echo mkdir -p $(dirname $dst)
  echo mv $src $dst
done < <(find /Pictures -name \*A.jpg)
Suppose the find command gives the only and unique result:
Code:
/Pictures/subdir/subsubdir/123A.jpg
inside the loop it will result in
Code:
dst=/Resized/subdir/subsubdir/123A.jpg
echo mkdir -p /Resized/subdir/subsubdir
echo mv /Pictures/subdir/subsubdir/123A.jpg /Resized/subdir/subsubdir/123A.jpg
which is exactly what you're looking for.
 
Old 04-08-2011, 04:54 PM   #24
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
ok, thanks. I'll give it a shot, even though I dont understand the substitution, like how it knows where to stop
 
Old 04-08-2011, 04:58 PM   #25
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
A little add-on. In case there are directory or file names with blank spaces, better to use double quotes:
Code:
while read src
do
  dst="${src/Pictures/Resized}"
  mkdir -p "$(dirname "$dst")"
  mv "$src" "$dst"
done < <(find /Pictures -name \*A.jpg)
 
Old 04-08-2011, 05:01 PM   #26
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
Thanks, i'll try that instead.

but i have a question. so what if my original path was /here/are/the/Pictures and i wanted to move the ones with *a.jpg to /i/want/them/here

how would i put that in that dst line? maybe that will help me understand how it works.
 
Old 04-08-2011, 05:29 PM   #27
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Indeed the substitution assumes that the two directories Pictures and Resized have the same path. Suppose you have a string like this:
Code:
/home/user/Pictures/something/some.jpg
and you want to transform it to
Code:
/home/user/Resized/something/some.jpg
if the original string is assigned to a variable (called src), you can replace the substring Pictures with the string Resized using
Code:
${src/Pictures/Resized}
that is by using the following syntax:
Code:
${variable/pattern/replacement}
Obviously if the two directories are not in the same path, you can try a slightly different approach. For example:
Code:
while read src
do
  dst=/i/want/them/here"${src/*Pictures/}"
  mkdir -p "$(dirname "$dst")"
  mv "$src" "$dst"
done < <(find /here/are/the/Pictures -name \*a.jpg)
In this case the replacement string in the substitution is null (that is it removes the first part of the path until the Pictures directory). Then the new path is prefixed to the result. This avoids confusion between the slashes used as delimiters in the substitution syntax and the slashes contained in the destination path.
 
Old 04-08-2011, 05:47 PM   #28
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Quote:
Originally Posted by rjo98 View Post
I tried the folder structure one, and it says "missing argument to '-exec' "
You will get this error if you don't add: \; to the end of the line.
 
Old 04-08-2011, 06:34 PM   #29
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by rjo98 View Post
I tried the folder structure one, and it says "missing argument to '-exec' "
Sorry, there was a thinko in the command. It's fixed now. The correct commands are
Code:
cd Pictures/
find ./ -type d -exec mkdir -p -- '../Resized/{}' '../Originals/{}' ';'
find ./ -name '*a.jpg' -exec cp -vi -- '{}' '../Resized/{}' ';'
find ./ -name '*[^a].jpg' -exec cp -vi -- '{}' '../Originals/{}' ';'

Last edited by Nominal Animal; 04-08-2011 at 06:38 PM.
 
Old 04-09-2011, 02:06 AM   #30
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
How about:
Code:
#!/bin/bash

RESIZE='Pictures/Resize'
ORIGINAL='Pictures/Original'

while read -r FILEPATH FILE
do
    if [[ $FILE =~ a.jpg ]]
    then
        TMP="$RESIZE${FILEPATH#*Pictures}"
        [[ -d $TMP ]] || mkdir -p "$TMP"
    else
        TMP="$ORIGINAL${FILEPATH#*Pictures}"
        [[ -d $TMP ]] || mkdir -p "$TMP"
    fi

    cp -v "$FILEPATH/$FILE" "$TMP/$FILE"
done< <(find Pictures/ -type f -iname '*[0-9a].jpg' -printf "%h %f\n")
This is presumed to being run from the directory above your Pictures directory. If you wish to make it a little more flexible you could force the user to enter the directory path
and set it within the script, like:
Code:
if (( $# != 1 ))
then
    echo "Usage: $0 <path to files>"
    exit 1
else
    DIR="$1"
fi

<snip>

done< <(find "$DIR" -type f -iname '*[0-9a].jpg' -printf "%h %f\n")
 
  


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
how to link two separate files nagananda Linux - Newbie 3 07-30-2009 04:05 AM
Parsing input into separate files bboyz Linux - Newbie 7 05-07-2008 02:08 AM
Encoding separate audio channels to separate files omnio Linux - Software 0 06-01-2007 07:46 AM
Looking for a tool to auto crop and separate images in to separate files.. mlsfit Linux - Software 2 08-06-2006 03:13 PM
Fortran and separate module files Neruocomp Programming 4 04-14-2005 08:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:38 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration