LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-14-2013, 08:22 AM   #1
user1111
LQ Newbie
 
Registered: Nov 2013
Posts: 4

Rep: Reputation: Disabled
move files or a directory to another directory using shell script


Hi all,
I need help in moving a file to a different folder using k-shell script. I am doing a file pattern search in a directory (two levels deep), and if found need to move that file to a different folder.

For eg.
input_dir = /opt/oracle/client1/
searching in /opt/oracle/client1/test1/work/
All the files with ".done" extension need to be moved to /opt/oracle/client1/test1/Done folder.
This is what I have so far

find "$input_dir" -type f -mindepth 3 -maxdepth 3 | while read filename;
do
#echo "$filename"
echo "$filename" | grep -q '.done' && echo "$filename is done".
done.

I don't know how I can move the file to the folder when I don't have a folder name. Thanks for the help.
 
Old 11-14-2013, 08:27 AM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
How about
Code:
input_dir=/opt/oracle/client1
output_dir=Done
for i in $input_dir/*/*/*.done; do
   loc=$(dirname "$i")
   dest=$(dirname "$loc")/$output_dir
   if [[ ! -d "$output_dir" ]]; then
      mkdir -p "$output_dir"
   fi
   mv "$i" "$output_dir"
done

Last edited by suicidaleggroll; 11-14-2013 at 08:33 AM.
 
1 members found this post helpful.
Old 11-14-2013, 08:41 AM   #3
user1111
LQ Newbie
 
Registered: Nov 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks for the quick reply. The only problem is that I have multiple folders in client which I forgot to mention in the original question.
Eg - /opt/oracle/client1/test1/A - file needs to be moved to opt/oracle/client1/test1/Done
/opt/oracle/client1/test2/A - file needs to be moved to opt/oracle/client1/test2/Done
/opt/oracle/client1/test3/A - file needs to be moved to opt/oracle/client1/test3/Done

Also, is there a way to detect if the file at that level is a file or a folder. If it is a folder, then the folder needs to be moved since it would have ".done" extension.
 
Old 11-14-2013, 09:02 AM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
I edited my code before you posted to dynamically build up the destination, it should take care of your first problem.

As for the file or directory, you can use the -f or -d checks, but it shouldn't matter, the above code should work regardless of the type.
 
1 members found this post helpful.
Old 11-15-2013, 12:03 AM   #5
user1111
LQ Newbie
 
Registered: Nov 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
It worked like a charm. Thanks!
If I also have to move the .NOTDONE files/folders to a NOTDONE folder, then should I write another similar loop or can it be taken care of in the same loop.

Thanks for all the help!
 
Old 11-15-2013, 04:59 AM   #6
user1111
LQ Newbie
 
Registered: Nov 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
I encountered an issue with this when I run it again. It is searching in the "Done" folder. Can we somehow exclude this folder in the search ?

Thanks!
 
Old 11-15-2013, 08:13 AM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
You could check if loc and output dir are the same before the move, should handle any files already in "Done".
 
  


Reply



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
Move odd files in a directory to a different directory kamaleshbv1 Linux - Newbie 6 08-06-2012 07:18 PM
[SOLVED] script to move files to a directory arturo393 Linux - General 6 08-13-2011 09:24 AM
Need to create a script to move files from one directory to another mustang05 Solaris / OpenSolaris 4 06-16-2006 10:25 AM
how to write a script to move files from one directory to another myeire Linux - Newbie 7 02-25-2005 10:01 AM
Does anyone know how to move files to a particular directory using shell scripting RowanB Programming 3 11-11-2004 01:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:39 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration