The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Auto copy for files from folder to folder upon instant writing Bashar UNIX for Advanced & Expert Users 2 08-21-2008 02:44 PM
moving a file to a new folder and automated ftp dineshr85 Shell Programming and Scripting 1 10-05-2007 10:24 AM
Parse the .txt file for folder name and FTP to the corrsponding folder. MeganP Shell Programming and Scripting 3 07-03-2007 01:54 PM
Take a folder name and find it in another folder (Complicated) hkhan12 Shell Programming and Scripting 5 09-06-2006 12:25 PM
Suppres error message when moving files from empty source folder Steven Shell Programming and Scripting 2 11-19-2001 01:25 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-06-2008
farooqpervaiz farooqpervaiz is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 7
Moving Files from one folder to another folder

Hi,

I have a folder which contain the log files. The folder may contain sub folders as well. I want to move the contents of the log folder to tmp folder periodically. I have used the command.

LOG_DIR=/logs

DESTINATION_DIR=/tmp/logs

find ${LOG_DIR} -mtime +1 -exec mv {} ${DESTINATION_DIR} \;

but, the problem is that this command moves all the files in the same folder and source folder structure (sub-folders) is not maintained in the destination directory.

Any ideas? or some other way to do this

Regards,
Muhammad Farooq
  #2 (permalink)  
Old 08-06-2008
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
add these options to your find command:
Code:
-maxdepth 0  -type f
  #3 (permalink)  
Old 08-06-2008
farooqpervaiz farooqpervaiz is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 7
No success, as it gives 'bad Option -maxdepth' error.

Actually, my scenerio is that I have a folder name 'Messages' and contains subfolders 'message1', 'message2', 'message3', 'message4' and so on. I want to move the folder Messages to the destination directory say 'Logs' and want that directory structure in the destination remains the same. But, when i use the above find command, all the files in the subfolder are placed in the same folder.
Is there any other way through which i can move files older than, say 5 days, to another location, while maintaining the directory structure of the source
  #4 (permalink)  
Old 08-06-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
How about this?

Code:
find Messages -type f -mtime +5 | while read file
do
    dest="Logs${file#Messages}"
    [ -d "$dest" ] || mkdir -p "$dest"
    mv "$file" "$dest"
done
  #5 (permalink)  
Old 08-06-2008
farooqpervaiz farooqpervaiz is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 7
Can anyone please explain what the following line is doing :

dest=$(basename "$file" | sed 's/Messages/Logs/')
[ -d "$dest" ] || mkdir -p "$dest"
  #6 (permalink)  
Old 08-06-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
Actually, I made a mistake. :-) I edited my post to correct it, but obviously wasn't fast enough. Anyway, my update was also incorrect...

Code:
    dest=$(dirname "$file" | sed 's/Messages/Logs/')
    [ -d "$dest" ] || mkdir -p "$dest"
Let's say you have a file, Messages/message3/someoldlogfile.log, dirname would pull out the "Messages/message3" component, and sed converts it to "Logs/message3".

The second line tests whether this directory exists, and if not, creates it.
  #7 (permalink)  
Old 08-06-2008
farooqpervaiz farooqpervaiz is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 7
thanks for reply,
what is this s for in 'sed s/Messages/Logs/'

Also, i have a source folder and destination folder in a variable, so how would i replace that in a above command

$SRC_DIR=/var/opt/Messages

probably shomething like below?
dest=$(dirname "$file" | sed 's/${SRC_DIR}/Logs/')

Last edited by farooqpervaiz; 08-06-2008 at 11:48 PM..
Sponsored Links
Closed Thread

Bookmarks

Tags
clear logs, find -maxdepth, logs, moving files

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:01 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0