LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Closed Thread
 
Search this Thread
Old 09-19-2012, 06:48 AM   #1
driftux
LQ Newbie
 
Registered: Sep 2012
Posts: 13

Rep: Reputation: Disabled
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?
 
Old 09-19-2012, 06:54 AM   #2
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 608

Rep: Reputation: 179Reputation: 179
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...

Last edited by 414N; 09-19-2012 at 06:55 AM.
 
Old 09-19-2012, 07:01 AM   #3
driftux
LQ Newbie
 
Registered: Sep 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
Thanks a lot! Ok, what do you think, how can it be improved?
 
Old 09-19-2012, 07:03 AM   #4
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,635

Rep: Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248
what about:
find /home/users/*/personal/*/docs/Z -type f -amin -10 -exec mv {} old_{} \;
 
Old 09-19-2012, 07:10 AM   #5
driftux
LQ Newbie
 
Registered: Sep 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
Just a minute I will try and let you know.
 
Old 09-19-2012, 07:15 AM   #6
driftux
LQ Newbie
 
Registered: Sep 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
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?
 
Old 09-19-2012, 07:26 AM   #7
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,635

Rep: Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248
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
 
Old 09-19-2012, 07:42 AM   #8
driftux
LQ Newbie
 
Registered: Sep 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
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...
 
Old 09-19-2012, 07:52 AM   #9
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,635

Rep: Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248
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
 
Old 09-19-2012, 08:04 AM   #10
driftux
LQ Newbie
 
Registered: Sep 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
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...
 
Old 09-19-2012, 08:17 AM   #11
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,635

Rep: Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248
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)
 
2 members found this post helpful.
Old 09-19-2012, 09:07 AM   #12
driftux
LQ Newbie
 
Registered: Sep 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
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?
 
Old 09-19-2012, 09:32 AM   #13
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,452

Rep: Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538
Quote:
Originally Posted by driftux View Post
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/
 
Old 09-19-2012, 10:58 AM   #14
driftux
LQ Newbie
 
Registered: Sep 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
Yes, TB0one i read it and I posted answer personally to you.
 
Old 09-19-2012, 11:11 AM   #15
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,452

Rep: Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538
Quote:
Originally Posted by driftux View Post
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.
 
1 members found this post helpful.
  


Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script at bottom of script - what am I missing slufoot80 Linux - Newbie 27 08-31-2012 10:03 PM
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
How to search for missing files and pass their names on to another shell script djslothario Linux - Newbie 3 08-07-2009 12:59 AM


All times are GMT -5. The time now is 10:09 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration