LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Shell script, what am I missing (http://www.linuxquestions.org/questions/linux-software-2/shell-script-what-am-i-missing-4175427970/)

driftux 09-19-2012 07:48 AM

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?

414N 09-19-2012 07:54 AM

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
The {} are placeholders for the filename "find" found.
Still, I don't get why you're performing those 2 nested "find"s...

driftux 09-19-2012 08:01 AM

Thanks a lot! Ok, what do you think, how can it be improved?

pan64 09-19-2012 08:03 AM

what about:
find /home/users/*/personal/*/docs/Z -type f -amin -10 -exec mv {} old_{} \;

driftux 09-19-2012 08:10 AM

Just a minute I will try and let you know.

driftux 09-19-2012 08:15 AM

So I tried, but something is missing in both examples:

1.
Quote:

for d in $(find /home/users/*/personal/*/docs/Z -type d); do find $d -maxdepth 1 -type f -amin -10 -exec mv {} old_{}\;; done
find: missing argument to `-exec'

2.
Quote:

find /home/users/*/personal/*/docs/Z -type f -amin -10 -exec mv {} old_{} \;
mv: cannot move `/home/users/*/personal/*/docs/Z/testfile.txt' to `old_/home/users/*/personal/*/docs/Z/testfile.txt': No such file or directory

Any ideas?

pan64 09-19-2012 08:26 AM

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
d=${file%/*}    # dirname
n=${file##*/}  # basename
case $n in
  old_*) ;;
  *) mv $d/$n $d/old_$n;;
esac


(not tested)
3. you should skip files already renamed

driftux 09-19-2012 08:42 AM

Thank you for help. I did that, but it seems that error from 1. goes to 2.
After I executed
Quote:

for d in $(find /home/users/*/personal/*/docs/Z -type d); do find $d -maxdepth 1 -type f -amin -10 -exec mv {} old_{} \; ; done
I got an error:
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...

pan64 09-19-2012 08:52 AM

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

driftux 09-19-2012 09:04 AM

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...

pan64 09-19-2012 09:17 AM

here is a small shell script, name it mv_file, and add execute flag
Code:

#!/bin/bash

# set -xv  # uncomment if you want to check how it works
file=$1
d=${file%/*}    # dirname
n=${file##*/}  # basename
case $n in
  old_*) ;;
  *) mv $d/$n $d/old_$n;;
esac

here is the find command:
Code:

find /home/users/*/personal/*/docs/Z -type f -amin -10 -exec <path_to_script>/mv_file {} \;
(if you really want to say thanks, press yes - bottom right corner)

driftux 09-19-2012 10:07 AM

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?

TB0ne 09-19-2012 10:32 AM

Quote:

Originally Posted by driftux (Post 4784153)
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?

Did you check your other thread, where you asked the same thing about your homework??
https://www.linuxquestions.org/quest...es-4175427831/

driftux 09-19-2012 11:58 AM

Yes, TB0one i read it and I posted answer personally to you.

TB0ne 09-19-2012 12:11 PM

Quote:

Originally Posted by driftux (Post 4784247)
Yes, TB0one i read it and I posted answer personally to you.

You mean this?
Quote:

Originally Posted by driftux
I don't know what is wrong with you men... Are you teacher of informatics at school or did you have a terrible childhood?
As I said I'm not a programmer, and I have looked all day in Google and I read a lot, but didn't find an answer. I am not going to learn all batch programming language because of one script.
I would better ask help, and next time provide my help to people here. If you are not able to write two rows of script to someone because of your ego, you should work more not with command line but with your personality.

And I'll post this here too:

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 05:52 AM.