You are not logged in.

1

Wednesday, April 28th 2004, 4:15pm

How to auto replace smaller files on copy.

Hello.
This is a file management question about Konqueror and I didn't know where else to ask.
Here's the deal. I have a bunch of files with the same names that are different sizes in different directories. I want to copy them into a single directory and automatically overwrite the smaller ones of the same name with the bigger ones. At the same time, I don't want the existing bigger ones to be overwritten if the new ones are smaller.
So, the idea is to create a single directory with only the big files and all the small files get overwritten whether they are in the source of the destination.
Can I do that in Konqueror or is this more of a job for a shell script? How would I write a shell script that would do that?
Or, where is a better place to ask.
Thanks

m4ktub

Intermediate

Posts: 257

Location: Lisbon, Portugal

Occupation: Software Engineer

  • Send private message

2

Wednesday, April 28th 2004, 6:14pm

With konqueror you can copy all files (using the View->View Mode->Tree View), do paste in the directory you want and choose skip or overwrite in the dialogs that appear (they contain the size of the files).

A possible script is something like
[code:1]#!/usr/bin/env tclsh

set destination [lindex $argv 0]
set files [lrange $argv 1 [llength $argv]]

foreach file $files {
set filename [file tail $file]
set target $destination/$filename

if {[file exists $target] && [expr [file size $target] >= [file size $file]]} {
puts "skipping $file"
continue
}

puts "copying $file"
file copy -force $file $target
}[/code:1]

to use it do something like
[code:1]<scriptname> <dir> <file 1> <file 2> ... <file n>[/code:1]

3

Wednesday, April 28th 2004, 7:30pm

I can't say I really grok what's going on in the script, but I sat around with the man pages on cp and I don't think I can do what I want with a cp command either.
Originally, I was hoping there was options in Konqueror that would allow me to set up this sort of simple logic, but it looks like the answer is no. In this case, manually using mouse clicks to sort though the files is not exactly a preferable option.
Let me give you an example of what I'm trying to do. I'm not saying this is what I'm doing, but it gives you an idea of what I'm trying to do.
Say two friends have MP3 collections that both have some partial files mixed in. They want to merge these collections of files and get rid of all the short ones. That's similar to what I'm trying to do.
So, in that case. Let's say that the two directories are called /home/Friend1 and /home/Friend2. Their target directory is called /home/MixedFriends.
Using those names, would I use your script like this?
<scriptname> /home/friend1 /home/friend2 /home/MixedFriends

4

Thursday, April 29th 2004, 7:29am

Ok, m4ktub, I'm thinking perhaps you got turned off by my example of two Mp3 collections. Or maybe you're just not a big hand holder for less than completely on-topic posts. Whatever the case, thanks for the initial help there. I'll play with your script on my own although the usage seems a bit odd the way you've presented it as I'm concerned with directory merging and specifically don't want to name files explicitly. I might just have better luck starting from scratch.

But to get closer to the topic of this board, let me put it another way. I recall doing something like this a long time ago with some Win95 tool. I don't remember the name. It was like PowerDesktop or something. It replaced the Win95 file manager with its own file manager and it had a feeature something like this. After all, this isn't such an unusual scenario. That's what I'm really looking for, and this forum should be a good place to ask for tips on where to find something like that.

Or perhaps I would be better off at the Gnome forums? :shock:

m4ktub

Intermediate

Posts: 257

Location: Lisbon, Portugal

Occupation: Software Engineer

  • Send private message

5

Thursday, April 29th 2004, 5:56pm

Quoted

Ok, m4ktub, I'm thinking perhaps you got turned off by my example of two Mp3 collections. Or maybe you're just not a big hand holder for less than completely on-topic posts.

Neither, I had network problems. My proxy fails somethimes.

The script copies files (not directories) to a destination directory. Although it only copies files you can use it anyway like this:
[code:1]FILES="`find /home/friend1 -iname *.mp3` `find /home/friend2 -iname *.mp3`"
<scriptname> /home/MixedFriends $FILES
[/code:1]
Translation:
The shell variable FILES will hold a list of files. This list is obtained by finding all files in /home/friend1 and /home/friend2 that have a name matching *.mp3.
Then the script copies to /home/MixedFriends all the found files overwritting the smaller ones.

Hope this is clear enough about the script and command.

Now for the semi-graphical way of doing this.
In konsole write
[code:1]<scriptname> /home/MixedFriends <space>[/code:1]
In konqueror chose "Tools->Find File". Choose the directory and *.mp3 as filter; find the files; select all the results and drag them to the konsole. Then press enter and voilá :-). Easier?

Ps: About konqueror. When using tree view you can chose files from multiple hierarquies adn copy them to a single directory. Then you would have "only" to choose yes or no with duplicate files. Of couse, it can be very anoying.

6

Friday, April 30th 2004, 11:17am

Whoa, I was in town for a day or so and didn't have time to check back. I'm so glad to get a response on this one. I'm going to give this a little run-down this evening. Thanks for the hand-holding here, I'd really like to be better at shell scripting besides just using like a DOS batch file and this is a great one to sink my teeth into. I'll let you know what happens.

7

Friday, April 30th 2004, 8:45pm

Oh man, I almost got it. But I think it's choking on the blank spaces on the file names.
At first I goofed it up by using the single quote " ' " rather than the backtick " ` " but I caught that and it started to work until it hit the a name that started with an A and then had a space.
Is there a way to make the script handle blank spaces? I can see that another alternative would be to convert the spaces into another character bfore running the script. Do you think that would be less trouble than modifying the script to handle blank spaces?
This might seem like a lot of hassle just to merge a couple of directories, but doing it manually is mind numbing. I'd rather spend three weeks trying to figure out how to automate it than three hours clicking dialogues. Besides, doing it manually all it takes is one itchy finger and you've nearly got to start from the beginning. Not pretty.
Well, I hope you can add a bit more advice, but I really appreciate what you've done for me so far. It's not as simple as I had hoped, but it looks like it's not completely out of reach.

8

Friday, April 30th 2004, 9:14pm

:D You are a saint my good man.

I went ahead and used a little GUI utility to change the names to underscores and it worked. It's awesome. Great stuff. I really appreciate the help.


Of course if there is a way to handle spaces in the script without too much trouble, I'd be interested to know how. But I don't want to trouble you to much because the heavy lifting is done.

Muchas gracias.