Script to delete duplicate files - Printable Version +- nixers (https://nixers.net) +-- Forum: Development & Graphics (https://nixers.net/Forum-Development-Graphics) +--- Forum: Programming On Unix (https://nixers.net/Forum-Programming-On-Unix) +--- Thread: Script to delete duplicate files (/Thread-Script-to-delete-duplicate-files) |
Script to delete duplicate files - pkal - 17-11-2017 Maybe someone besides me can make use of this script I wrote. I use it to delete duplicate files (hence it's called deldup) in the current and all contained directories. In my case it's been particularly helpful in cleaning up my .pdf collection, containing multiple file with the same content (ie. sam MD5 hashsum), but either different file names or just placed twice in two directories. It then offers a selectable list of items to delete. Typing "!" will delete every file with a certain hashsum. I've only tested it under void with bash, but I'd like to make it as portable as possible, this means replacing all bash tricks (readarray, let, regular expression matching) with some POSIX compatible equivalents. And here's the script: Code: #!/bin/bash Alternative uploads: https://a.pomfe.co/udnjib, https://sub.god.jp/f/F1D3s2iQ.sh, https://comfy.moe/ypqbun I never had any actual formal introduction to shell scripting, so this isn't the tidiest script. I wrote it over a timespan of a few months, adding features as I needed them. Feedback would be much appreciated. Also, is there a simpler way do do this under Unix? Maybe even a pre-existing tool? RE: Script to delete duplicate files - z3bra - 18-11-2017 As you learnt scripting on your own, it's rather good, so kuddos for that! Over the years, I've written a bunch of scripts, and still use a most of them on a day to day basis, either interactively, in pipes or through cronjobs. Here are a few tips I can give you, based on my experience: 0. Think small. Scripts are supposed to glue programs together, not be programs on their own. Think your ouptut to be useful to other programs. 1. Be quiet. Logging looks cool, especially with "...." and green "OK" or red "FAIL", but they definitely don't help in pipelines. 2. Avoid using 'rm' in shell scripts. I tend to make my script be selectors or filters, so that I can inspect the output beforhand, and append "xargs rm" after that. 3. Avoid interactivity. The best tools are the one that are automated and can "think" on their own. Note that these tipa are totally subjective, it's based on my own experience, so it might differ for other people. Now, I know that these are rather abstract, so here is an attemot at doing the same, "my way" ;) (written directly from memory, without testing of course :P) Code: #!/bin/sh This basically only transfom the input, and let the filesystem untouched, so even if the script is messed up, I cannot delete any file, loose data or corrupt it (because I know how bad I can be at coding :P) Anyway, keep scripting! EDIT: OMG, it works! RE: Script to delete duplicate files - pkal - 18-11-2017 But why should shell scripts be treated differently from other programs in my path? To me it's precisely that I don't know and don't have to know how a program works, how it's implemented, etc. that I find interesting about a unix environment. For some reason discriminating against shell scripts being interactive or deleting files (One can't argue te user doesn't know, he has to consciously call the script and give it valid input), seems arbitrary. And regarding your script, I remember doing something lile that before I had this script, but I didn't always want to delete the first file it found, but I had to manually scan the output and run rm for those files I wanted to delete. Saying that writing a script that helps me automate the process by glueing the steps together is bad style (especially if I know that it's output is never parsed nor has to be parsed by other programs), seems wierd. RE: Script to delete duplicate files - z3bra - 19-11-2017 I'm not saying you should do it my way. If you felt offended or whatever, I'm sorry, that was definitely not the point. I just wanted to discuss your style. I use interractive shell scripting a lot actually, and I tend to "improvise" them within interactive sessions, rather than saving them in my toolbox. Sometimes the logic is just too complex or to random, and interactive use is indeed best. The fact I don't find shell scripting suitable for "real" programs, is because its structure is too simple, and not safe enough. It also lacks some basic constructs that make it hard tl deal woth complex data structure. I'm talking about POSIX shell only. I won't mention bashisms et al. here because I don't want too :) There are no array types, quote escaping seems random, and implementation specific, arithmetic is barely handled, ... Add on top on that the builtin VS. binary problem and you get a language that is a pain to "program" in. In my mind, shell scripting should be there to help external programs interact with each others, not be a program by itself. That being said, it's a really simple language that has the biggest "libraries" available through external programs, so it is definitely the quickest way to get started with programming! RE: Script to delete duplicate files - pkal - 19-11-2017 I wasn't offended, it's just that I generally really appreciate your opinion, and was surprised when I read your post. All I'm saying is that I don't see a problem in using shell scripts to solve simple tasks (like this one). I'd be the last person (well I'm actually not sure ;^D) who would want to write a physical simulation, a compiler or a browser in shell script, since it obviously the shell isn't the right tool for the task. To quote the Master Foo: Quote:“There is more Unix-nature in one line of shell script than there is in ten thousand lines of C.” RE: Script to delete duplicate files - budRich - 19-11-2017 That quote <3 I just uploaded a 1000 lines long bash script to github . There is only one rule to shell scripting: get it done. I haven't tried your script, because i believe every file is sacred. I like your style. z3bra style is also good. RE: Script to delete duplicate files - z3bra - 20-11-2017 Don't get me wrong, shell scripting is perfectly suited for your use case! I think that different people solve problems in different ways, and I like to see multiple solutions to each problems myself. That's why I wanted to share my POV, because I think it differs significantly from your, so it could be interresting to other people reading this thread. I'm not saying my way is better than your (even though I OBVIOUSLY prefer mine :P), just that I think differently, and though it was cool to have different approached to this. The only important thing here is wether or not your script solved your issue ;) RE: Script to delete duplicate files - budRich - 20-11-2017 Great approach z3bra. Scripting style is as personal as one's handwriting. And it's cool that we are different. I will make a separate thread and link to my mentioned script (don't want to hijack this thread more then necessary and such). It would be interesting to get some input on the style. I know I do a lot of things.. in a not codesher way, and i am a proud bashist... RE: Script to delete duplicate files - venam - 20-11-2017 It seems like an endless quest. Some years ago everyone was writing bots, kind of like a rite of passage. It also seems like everyone was making a duplicate remover. Etc... Now we're back at writing bots. And you're bringing back duplicate removers. Jokes aside, I've also written one of those, in Perl this time: https://github.com/venam/duplicate-remover I don't remember at all the quality of the code though, it might be bad (warning). There are probably more efficient ways to solve this taking edge cases in consideration like links (hard/soft). EDIT: this is probably the total opposite of what z3bra proposes, full of colors and block characters, can't be used in a pipeline. RE: Script to delete duplicate files - budRich - 20-11-2017 @venam: I don't really know perl. But your script is very readable, well documented and good comments, I wish more (public) scripts was like that. I have started to write the documentation/help (SCRIPT -h) first and then write the code needed to make the description valid. It's a good way to trick my trickster brain into not frankensteining my scripts. RE: Script to delete duplicate files - z3bra - 20-11-2017 (20-11-2017, 09:40 AM)venam Wrote: It seems like an endless quest. We all used the same projects to either get started with programming, or learn a language. Be it a task manager, IRC bot, text editor, ... The thing I like best for that, is code golfing though. You always find someone doing things in a more clever way than you do, stripping some bytes along the way. I really enjoyed all the challenges we used to make this way. That's probably why I felt obliged to propose something else here ;) |