How Create new directory and move files to that directory.?

Hi All,

We have main directory called "head"
under this we have several sub directories and under these directories we have sub directories.

My requirement is I have to find the SQL files which are having the string "procedure" under "head" directory and sub directories as well.
And create new directory "procedure" under "head" and move all the files to the new directory.

head/procedure

In the "procedure" directory all the SQL files which contain the string "procedure" should be there.

Please help me.

Thanks.

Sounds like a one off action for me, not a regular repetitive task for a script. Where do you need help?
mkdir to create directory.
grep to look for keywords in files, evtl. with the -r option.
mv to move the files found.

Hi ,

I have tried this but its not working getting error.

mkdir procedure
find . -name "*.sql"  -exec grep -l 'procedure' {} \; | mv {} /head/procedure

Thanks

---------- Post updated at 12:48 PM ---------- Previous update was at 12:32 PM ----------

Hi,

Please help me.

Thanks

WHAT is not working? WHAT is the error?

Hi,

getting the error

mv: cannot access {}

Thanks.

Building on what you learned from your last thread (How to search for a string in all the files irrespective of directory.?) from suggestions by Scrutinizer and RavinderSingh13, it would seem that the following should come close to doing what you want.

I note that your 1st post in this thread says you're looking for SQL files, but in post #3 in this thread your code is looking for sql files. The following assumes that your files follow the convention used in post #3. In the future, please state your requirements clearly in your first post. Changing requirements with each new post in a thread wastes everyone's time.

In the future, PLEASE tell us what operating system and shell you're using, so volunteers here don't waste their time making suggestions that won't work in your environment. We should not have to search through dozens of your earlier posts to guess at your environment so we can help you each time you start a new thread.

#!/bin/ksh
head=/absolute/path/to/head

mkdir -p "$head/procedure"
find "$head" \( -type d -name procedure -prune \) -o \
    \(	-type f -name '*.sql' -exec grep -q -F 'procedure' {} \; \
    	-exec mv {} "$head/procedure" \; \)

Obviously, you'll need to change /absolute/path/to/head in the above script to the actual absolute pathname of the head directory you want to use. (You didn't give a pathname for this directory in your first post and the 3rd post in this thread gives conflicting locations for it.)

The {} placeholder has a meaning for the find command alone. After piping find 's output to the mv command, the file name found are supplied via stdin. You'll need something to convert stdin to parameters on the command line, xargs .

find . -name "*.sql"  -exec grep -l 'procedure' {} \; | xargs mv  -t /head/procedure

BUT - this may lead to problems as in file names with white space.

Hi Don,

I have tried the below script and no files moved to the directory "procedure"
even we have SQL files with string procedure.

path for head is /home/head

#!/bin/ksh
head=/home/head

mkdir -p "$head/procedure"
find "$head" \( -type d -name procedure -prune \) -o \
    \(	-type f -name '*.sql' -exec grep -q -F 'procedure' {} \; \
    	-exec mv {} "$head/procedure" \; \)

Please helpme.

Thanks

Did you get any diagnostic messages when you ran the script?

What operating system are you using?

Please show us the absolute pathname of an SQL file with a name ending in .sql that contains the string procedure . Show us the results from the commands:

ls -l path
grep procedure path

where path is the absolute pathname of one of the files that is not being moved by the script I suggested.

Hi don,

Please find the details as below

Version M-11/16/88i

Korn shell

We have sql files with the string procedure in the follwoing paths.

/head/sql
head/db

Thanks

---------- Post updated at 06:01 PM ---------- Previous update was at 04:07 PM ----------

Hi Don,

Can u please help me.

Thanks

I will be happy to help you if you provide the data I requested in post #9 in this thread. Without an example showing what is misbehaving, I can't help. The code I suggested works perfectly with the sample data I created on my system to test what I understood to be your requirements. (But, bumping up posts and thinking that I should be on call 24 hours/day to solve your problems is unacceptable.)

And, PLEASE tell us what operating system you're using! Version M-11/16/88i is not an operating system (although it might be a version number or a release number of an operating system). If you don't know what operating system you're using, please show us the output from the command:

uname -a

Hi Don,

This is the version
SunOS 5.10 Generic_147440-13 sun4u sparc SUNW

Please help me.

Thanks

In post #9 in this thread, I said:

Until you provide this information, there is nothing we can do to help!

Hi Don,

/head/sql>ls -l /head/sql 

I got lot of files

/head/sql>grep procedure /head/sql

it's got giving any files.

/head/sql>grep procedure *.sql

It's giving files

Please help me.

Thanks

Hi ROCK_PLSQL ,
When you ran the script I suggested, did it produce any output at all? If so, exactly what output did it produce?

For the third time: Please give us the full pathname of just one file that is located somewhere in the file hierarchy rooted in /head that IS NOT located under /head/procedure that contains the string procedure and has a name ending with .sql after you ran the script I suggested AND show us the output from the command:

grep procedure /absolute/pathname

where /absolute/pathname is replaced by the absolute pathname of that file. (Please use CODE tags when showing us that output.)

Then also show us the output from the commands:

ls -l script
od -bc script

where script (in both places) is replaced by the absolute pathname of the file that contains the script I suggested. (Again showing us that output in CODE tags.)

And, please show us (in CODE tags) the exact command you issued to invoke the script I suggested.