FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Gentoo > Gentoo User

 
 
LinkBack Thread Tools
 
Old 06-25-2008, 03:31 PM
Etaoin Shrdlu
 
Default Quick script request

On Wednesday 25 June 2008, 17:08, Grant wrote:

> Feel free to ignore me here, but if anyone could whip out a quick
> script for this I would really appreciate it.
>
> I need to move any files from dir1 to dir2 if they don't already exist
> in dir2 with a slightly different filename. The dir1 files are named
> like a-1.jpg and the dir2 files are named like a-1_original.jpg.

Try this (untested)

#!/bin/bash

cd /dir1

for i in *; do
if [ ! -f "/dir2/$i" ]; then
bn=${i%.*}
ext=${i##*.}
nn=${bn}_original.${ext}
mv -- "$i" "/dir2/$nn"
fi

done


For each file in dir1, say a-1.jpg, this look if a file with the same
name exists in dir2. If not, it moves it to dir2 with the new name of
a-1_original.jpg. I hope I got that right.
--
gentoo-user@lists.gentoo.org mailing list
 
Old 06-25-2008, 03:32 PM
Alan McKinnon
 
Default Quick script request

On Wednesday 25 June 2008, Grant wrote:
> Feel free to ignore me here, but if anyone could whip out a quick
> script for this I would really appreciate it.
>
> I need to move any files from dir1 to dir2 if they don't already
> exist in dir2 with a slightly different filename. The dir1 files are
> named like a-1.jpg and the dir2 files are named like
> a-1_original.jpg.
>
> - Grant

rough and ready, off the top of my head:

cd dir1
for i in *jpg
do
j = basename $i .jpg
cp -u ${j}.jpg dir2/${j}_original.jpg
done

'cp -u' works around the messy problem of checking if the destination
file exists

--
Alan McKinnon
alan dot mckinnon at gmail dot com

--
gentoo-user@lists.gentoo.org mailing list
 
Old 06-25-2008, 03:37 PM
Grant
 
Default Quick script request

>> Feel free to ignore me here, but if anyone could whip out a quick
>> script for this I would really appreciate it.
>>
>> I need to move any files from dir1 to dir2 if they don't already
>> exist in dir2 with a slightly different filename. The dir1 files are
>> named like a-1.jpg and the dir2 files are named like
>> a-1_original.jpg.
>>
>> - Grant
>
> rough and ready, off the top of my head:
>
> cd dir1
> for i in *jpg
> do
> j = basename $i .jpg
> cp -u ${j}.jpg dir2/${j}_original.jpg
> done
>
> 'cp -u' works around the messy problem of checking if the destination
> file exists

Thanks guys, can you tell me how to execute this? Put it in a file
and './file' I think? Should I have special stuff at the top of the
file?

- Grant
--
gentoo-user@lists.gentoo.org mailing list
 
Old 06-25-2008, 03:47 PM
Alex Schuster
 
Default Quick script request

Grant asks:

> Thanks guys, can you tell me how to execute this? Put it in a file
> and './file' I think? Should I have special stuff at the top of the
> file?

Yes, a '#!/bin/bash', it you want top have thsi as a script. You need to
make it executable, too: chmod +x file

But you can also leave the #!/bin/bash out,l and call it like this: '. file'
or 'source file'. This acts as if you typed the commands directly in the
terminal.

Which would be the easiest way to do it: Just mark the stuff in your mail
program, ans paste it into you terminal.

Wonko
--
gentoo-user@lists.gentoo.org mailing list
 
Old 06-25-2008, 04:16 PM
Alan McKinnon
 
Default Quick script request

On Wednesday 25 June 2008, Grant wrote:
> >> Feel free to ignore me here, but if anyone could whip out a quick
> >> script for this I would really appreciate it.
> >>
> >> I need to move any files from dir1 to dir2 if they don't already
> >> exist in dir2 with a slightly different filename. The dir1 files
> >> are named like a-1.jpg and the dir2 files are named like
> >> a-1_original.jpg.
> >>
> >> - Grant
> >
> > rough and ready, off the top of my head:
> >
> > cd dir1
> > for i in *jpg
> > do
> > j = basename $i .jpg
> > cp -u ${j}.jpg dir2/${j}_original.jpg
> > done
> >
> > 'cp -u' works around the messy problem of checking if the
> > destination file exists
>
> Thanks guys, can you tell me how to execute this? Put it in a file

yes

> and './file' I think?

Either that or chmod a+x file and execute it directly

> Should I have special stuff at the top of the
> file?

#!/bin/bash

--
Alan McKinnon
alan dot mckinnon at gmail dot com

--
gentoo-user@lists.gentoo.org mailing list
 
Old 06-25-2008, 05:16 PM
Grant
 
Default Quick script request

>> >> Feel free to ignore me here, but if anyone could whip out a quick
>> >> script for this I would really appreciate it.
>> >>
>> >> I need to move any files from dir1 to dir2 if they don't already
>> >> exist in dir2 with a slightly different filename. The dir1 files
>> >> are named like a-1.jpg and the dir2 files are named like
>> >> a-1_original.jpg.
>> >>
>> >> - Grant
>> >
>> > rough and ready, off the top of my head:
>> >
>> > cd dir1
>> > for i in *jpg
>> > do
>> > j = basename $i .jpg
>> > cp -u ${j}.jpg dir2/${j}_original.jpg
>> > done
>> >
>> > 'cp -u' works around the messy problem of checking if the
>> > destination file exists
>>
>> Thanks guys, can you tell me how to execute this? Put it in a file
>
> yes
>
>> and './file' I think?
>
> Either that or chmod a+x file and execute it directly
>
>> Should I have special stuff at the top of the
>> file?
>
> #!/bin/bash

I put the above script in a file, added the appropriate header, issued
chmod, and when I execute with ./file I get a bunch of these:

./script: line 6: j: command not found
cp: cannot stat `.jpg': No such file or directory

- Grant
--
gentoo-user@lists.gentoo.org mailing list
 
Old 06-25-2008, 05:25 PM
Alex Schuster
 
Default Quick script request

Grant asks:

> >> > cd dir1
> >> > for i in *jpg
> >> > do
> >> > j = basename $i .jpg
> >> > cp -u ${j}.jpg dir2/${j}_original.jpg
> >> > done
> >> >
> >> > 'cp -u' works around the messy problem of checking if the
> >> > destination file exists
[...]
> I put the above script in a file, added the appropriate header, issued
> chmod, and when I execute with ./file I get a bunch of these:
>
> ./script: line 6: j: command not found
> cp: cannot stat `.jpg': No such file or directory

This:
j = basename $i .jpg
should be more like this:
j=$( basename $i .jpg )

Or: j=${i%.jpg}

That is, there must be no whitespace around the '='. And in order to set j
to the result of a command, use $( command ) or ` command `.

Wonko
--
gentoo-user@lists.gentoo.org mailing list
 
Old 06-25-2008, 07:34 PM
Grant
 
Default Quick script request

>> >> > cd dir1
>> >> > for i in *jpg
>> >> > do
>> >> > j = basename $i .jpg
>> >> > cp -u ${j}.jpg dir2/${j}_original.jpg
>> >> > done
>> >> >
>> >> > 'cp -u' works around the messy problem of checking if the
>> >> > destination file exists
> [...]
>> I put the above script in a file, added the appropriate header, issued
>> chmod, and when I execute with ./file I get a bunch of these:
>>
>> ./script: line 6: j: command not found
>> cp: cannot stat `.jpg': No such file or directory
>
> This:
> j = basename $i .jpg
> should be more like this:
> j=$( basename $i .jpg )
>
> Or: j=${i%.jpg}
>
> That is, there must be no whitespace around the '='. And in order to set j
> to the result of a command, use $( command ) or ` command `.

Worked perfectly, thanks a lot everyone.

- Grant
--
gentoo-user@lists.gentoo.org mailing list
 
Old 06-25-2008, 08:42 PM
Alan McKinnon
 
Default Quick script request

On Wednesday 25 June 2008, Grant wrote:

> > This:
> > j = basename $i .jpg
> > should be more like this:
> > j=$( basename $i .jpg )
> >
> > Or: j=${i%.jpg}
> >
> > That is, there must be no whitespace around the '='. And in order
> > to set j to the result of a command, use $( command ) or ` command
> > `.
>
> Worked perfectly, thanks a lot everyone.

Isn't there a little known and unused but very useful command that
already does this? This type of usage often comes up on mailing lists
and invariably someone mentions it after 20 posts or so, but I can
never remember what it is. Used a lot like rename, but that's not it.


--
Alan McKinnon
alan dot mckinnon at gmail dot com

--
gentoo-user@lists.gentoo.org mailing list
 
Old 06-26-2008, 01:42 AM
Daniel Iliev
 
Default Quick script request

On Wed, 25 Jun 2008 22:42:47 +0200
Alan McKinnon <alan.mckinnon@gmail.com> wrote:


> Isn't there a little known and unused but very useful command that
> already does this? This type of usage often comes up on mailing lists
> and invariably someone mentions it after 20 posts or so, but I can
> never remember what it is. Used a lot like rename, but that's not it.


I believe you are talking about the "rename (1)" command, but this
case is a little bit different.


--
Best regards,
Daniel
--
gentoo-user@lists.gentoo.org mailing list
 

Thread Tools




All times are GMT. The time now is 06:46 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org