bash: search out files by extension, remove spaces, copy elsewhere
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
thanks for providing, i just tried on a directory tree with about 2,000 images and i am getting those errors too. for you it mite be because of the # not sure what my issue is (also not sure how to correct) ?
i think the problem is my command chokes if any filename (image or not) has an apostrophe ' in it... i think it gets interpreted as a close quote.
Hmmm. Interesting possibility but I've scanned the file names of my images and not found any apostrophes. I really understand very poorly how tr works, but is it possible an apostrophe, under certain conditions (say, double-space), might be introduced by it? Thanks.
I found a better way, using $RANDOM (in place of $(date +%N)), to alter each symlink name so that files sharing the same name will not overwrite each other (credit to http://www.cyberciti.biz/faq/bash-sh...andom-numbers/). The latest incarnation of this one-liner is
I can't see why a 3-digit random sequence would not be sufficient for my project, despite the fact that I will be processing thousands of files; maybe a 2-digit sequence would even suffice. In any case, it's easy, by changing 900+99 to increase or decrease the number of digits to one's liking.
Thanks for the suggestion, ntubski. I have to say I like the $RANDOM solution better, since it prepends a set and limited number of digits/characters to the front of each file name, making it a simple matter of stripping those off to find the original file name. I'll test out your iteration to see whether it addresses the error message I was occasionally seeing when using the original script. I also discovered when testing out the original script again yesterday, that the image/ part of that iteration was producing some false positives, for example if a file or directory name had the word "image" in it, or if the file was an iso. Again, your input is much appreciated.
I can't see why a 3-digit random sequence would not be sufficient for my project, despite the fact that I will be processing thousands of files; maybe a 2-digit sequence would even suffice.
It only depends on how many files with the same name you have. Just a warning, if you have several files all with the same name the Birthday "paradox" applies, so you may need more digits than you think.
Thanks for pointing out the birthday paradox, ntubski. A quick check reveals that I might initially be dealing with something like 15k-20k files (a number later to be drastically reduced once target files within that group have been identified). I'm not so proficient with mathematics, so I'll have to do some further investigation as to whether my 3-digit scheme would suffice, given the total number of files I'll initially be dealing with.
I've fiddled around with your script, ntubski, trying to get an idea of how it works. My first observation is that it does not produce valid symlinks, probably because it is not recording the full path. If I replace the period with `pwd`, however, I do get valid symlinks. I also managed to splice in my echo $(</dev/urandom tr -dc A-Za-z0-9 | head -c3) to see whether I could manage that and got it working. One drawback to your script, unlike a more recent iteration of the one schneidz contributed, is that it leaves extraneous spaces in file names: his latest variant replaces those spaces with dashes. I also need to get rid of hash symbols that appear in some file names, but so far I've managed to do that by running
for file in *; do mv "$file" `echo $file | sed 's/#/Num/g'` ; done
in the directory where the symlinks have been placed. Anyway, thanks for helping me get a better grasp on how to execute this project.
I've fiddled around with your script, ntubski, trying to get an idea of how it works. My first observation is that it does not produce valid symlinks, probably because it is not recording the full path. If I replace the period with `pwd`, however, I do get valid symlinks.
Oh, I guess I didn't fully understand the layout of your files. When I tested here, the symlinks were valid.
One drawback to your script, unlike a more recent iteration of the one schneidz contributed, is that it leaves extraneous spaces in file names: his latest variant replaces those spaces with dashes. I also need to get rid of hash symbols that appear in some file names,
I didn't realize that was part of the requirements, it's easily added.