![]() |
Shell script, what am I missing
Hello, I have a script
for d in $(find /home/users/*/personal/*/docs/Z -type d); do find $d -maxdepth 1 -type f -amin -10; done It will list all files in Z directory created in period of 10 minutes. I am looking a way how do that all these searched files matching criterias will be renamed by adding prefix old_. I mean if script finds files aaaa and bbbb, then renames them to old_aaa and old_bbbb. Could someone help me? |
Simply add an exec command for find inside the inner find:
Code:
for d in $(find /home/users/*/personal/*/docs/Z -type d); do find $d -maxdepth 1 -type f -amin -10 -exec mv {} old_{}\;; done Still, I don't get why you're performing those 2 nested "find"s... |
Thanks a lot! Ok, what do you think, how can it be improved?
|
what about:
find /home/users/*/personal/*/docs/Z -type f -amin -10 -exec mv {} old_{} \; |
Just a minute I will try and let you know.
|
So I tried, but something is missing in both examples:
1. Quote:
2. Quote:
Any ideas? |
1. you should add a space after {}:
for d in $(find /home/users/*/personal/*/docs/Z -type d); do find $d -maxdepth 1 -type f -amin -10 -exec mv {} old_{} \; ; done 2. yes, that must be fixed: find /home/users/*/personal/*/docs/Z -type f -amin -10 -exec mv_file {} \; here mv_file is a script: Code:
file=$1 (not tested) 3. you should skip files already renamed |
Thank you for help. I did that, but it seems that error from 1. goes to 2.
After I executed Quote:
mv: cannot move `/home/users/*/personal/*/docs/Z/testfile.txt' to `old_/home/users/*/personal/*/docs/Z/testfile.txt': No such file or directory I'm trying different variants, but can't write complete script without erros... |
yes, that will fail, because {} contains the full path. You need to replace the mv command with a small script, what I posted (for example)
that script will split {} to dir and filename and will insert old_ before the filename |
You helped me a lot thanks for that very much. But I'm sitting by the server now and I'm not able to insert it properly...
|
here is a small shell script, name it mv_file, and add execute flag
Code:
#!/bin/bash Code:
find /home/users/*/personal/*/docs/Z -type f -amin -10 -exec <path_to_script>/mv_file {} \; |
YES!!! It's working!!! Thank you very much one more time! Of course I will push every button you need :)) I just need to find it, because I just registered here yesterday :)
The last question if I need to run script with CRON is there any way to combine these to parts together and make single script or just use in the way you recommend? |
Quote:
https://www.linuxquestions.org/quest...es-4175427831/ |
Yes, TB0one i read it and I posted answer personally to you.
|
Quote:
Quote:
This is your homework, not ours. YOU need to learn, but don't seem to want to. Again, we are not going to do your homework for you. And if it's just two lines, why can't you write it? Why would you need to "learn all batch programming language", for a two-line script?? If you really looked all day in Google and can't figure out a two-line script...you should consider dropping whatever class you're taking, or let the teacher know what problems you're having. Adding to it...pan64 essentially did your homework for you. And you're STILL asking for more...can't you put ANY effort into this? Can you even TRY to assemble the pieces someone else gave you?? If you truly are that lazy, why are you even bothering with classes? Because if you don't put effort into THIS lesson, you will not be prepared for the NEXT, HARDER lessons, and will be right back to asking people to do THAT for you too. |
All times are GMT -5. The time now is 02:53 PM. |