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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How Create new directory and move files to that directory.?
# 1  
Old 05-02-2016
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.
# 2  
Old 05-02-2016
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.
# 3  
Old 05-02-2016
Hi ,

I have tried this but its not working getting error.

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

Thanks

Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!


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

Hi,

Please help me.

Thanks

Moderator's Comments:
Mod Comment Don't bump up posts!

Last edited by RudiC; 05-02-2016 at 04:35 AM.. Reason: Added code tags.
# 4  
Old 05-02-2016
WHAT is not working? WHAT is the error?
# 5  
Old 05-02-2016
Hi,

getting the error

Code:
mv: cannot access {}


Thanks.

Moderator's Comments:
Mod Comment N O - N O N S E N S E : Please use code tags for code and data as required by forum rules!

Last edited by RudiC; 05-02-2016 at 04:47 AM.. Reason: Added code tags again.
# 6  
Old 05-02-2016
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.

Code:
#!/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.)
# 7  
Old 05-02-2016
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.
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

2. UNIX for Dummies Questions & Answers

How to move gz files from one source directory to destination directory?

Hi All, Daily i am doing the house keeping in one of my server and manually moving the files which were older than 90 days and moving to destination folder. using the find command . Could you please assist me how to put the automation using the shell script . ... (11 Replies)
Discussion started by: venkat918
11 Replies

3. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

4. Shell Programming and Scripting

Move files in a list to another directory

I have a number of files in a directory that can be grouped with something like "ls | grep SH2". I would like to move each file in this list to another directory. Thanks (4 Replies)
Discussion started by: kg6iia
4 Replies

5. UNIX for Dummies Questions & Answers

Zip all files in a directory and move to another directory

Hi, need to zip all files in a directory and move to another directory after the zip.. i am using this one but didnt help me... zip -r my_proj_`date +%Y%m%d%H%MS`.zip /path/my_proj mv in_proj_`date +%Y%m%d%H%M%S`.zip /path/source/ i am trying to zip all the files in my_proj... (0 Replies)
Discussion started by: dssyadav
0 Replies

6. UNIX for Dummies Questions & Answers

How to move files between 2 dates from one directory to another

Hi All, I am coding for a requirement where I need to move files (filename.yymmdd) from one directory(A) to another(B) based on 2 date fields in a paramtere file. (Paramfile.txt) For e.g: In Paramfile.txt, BUS_DT =20120612 SUB_DT =20120602 In this case, i need to move all the files... (14 Replies)
Discussion started by: dsfreddie
14 Replies

7. Shell Programming and Scripting

Move the latest or older File from one directory to another Directory

I Need help for one requirement, I want to move the latest/Older file in the folder to another file. File have the datetimestamp in postfix. Example: Source Directory : \a destination Directory : \a\b File1 : xy_MMDDYYYYHHMM.txt (xy_032120101456.txt) File2: xy_MMDDYYYYHHMM.txt... (1 Reply)
Discussion started by: pp_ayyanar
1 Replies

8. Shell Programming and Scripting

Move a file from windows directory to unix directory

Move a file from windows directory to unix directory, is this possible? if it is, can someone help me on this? Thanks! God bless! (1 Reply)
Discussion started by: kingpeejay
1 Replies

9. UNIX for Dummies Questions & Answers

Move all files in a directory tree to a signal directory?

Is this possible? Let me know If I need specify further on what I am trying to do- I just want to spare you the boring details of my personal file management. Thanks in advance- Brian- (2 Replies)
Discussion started by: briandanielz
2 Replies
Login or Register to Ask a Question