Moving Files from one folder to another folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving Files from one folder to another folder
# 1  
Old 08-06-2008
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  
Old 08-06-2008
add these options to your find command:
Code:
-maxdepth 0  -type f

# 3  
Old 08-06-2008
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  
Old 08-06-2008
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  
Old 08-06-2008
Can anyone please explain what the following line is doing :

dest=$(basename "$file" | sed 's/Messages/Logs/')
[ -d "$dest" ] || mkdir -p "$dest"
# 6  
Old 08-06-2008
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  
Old 08-07-2008
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-07-2008 at 12:48 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

2. Shell Programming and Scripting

Moving files and folders to another folder

I recently bought Synology server and realised it can run scripts. I would need fairly simple script which moves all files and folders from ARCHIVE folder to WORKING folder. I would also need to maintain folder structure as each of the folders may contain subfolders. How would I go about it as I am... (1 Reply)
Discussion started by: ###
1 Replies

3. UNIX for Dummies Questions & Answers

Moving files to a folder with a variable name

hello there- first post here- maybe someone can help- Basically I am trying to copy the contents of a folder to a different folder that has a variable name. the content I want to copy would be in a folder on my desktop called: myfolder the variable folder would look something like: ... (3 Replies)
Discussion started by: infothelonghaul
3 Replies

4. Shell Programming and Scripting

Finding files with wc -l results = 1 then moving the files to another folder

Hi guys can you please help me with a script to find files with one row/1 line of content then move the file to another directory my script below runs but nothing happens to the files....Alternatively Ca I get a script to find the *.csv files with "wc -1" results = 1 then create a list of those... (5 Replies)
Discussion started by: Dj Moi
5 Replies

5. Shell Programming and Scripting

Need help to write a script for moving the log files to some other folder

Hi Experts, I want to write a script, based upon the following requirement 1) I am having 5 application $ cd logs $ ls -l drwxr-xr-x 2 natraj nat 5.0K Sep 20 10:25 one drwxr-xr-x 2 natraj nat 5.0K Sep 20 10:39 two drwxr-xr-x 2 natraj nat 1.5K Sep 20 10:58... (4 Replies)
Discussion started by: natraj005
4 Replies

6. Shell Programming and Scripting

moving files from one folder to many folders

I have a more than 10 K files in a folder. They are accumulated in a period of more than an year (Say from 13th July 2010 to 4th June 2011). I need to perform housekeeping on this. The requirement is to create a folder like 13Jul2010,14July2010,......3June2011,4June2010 and then from the main... (2 Replies)
Discussion started by: realspirituals
2 Replies

7. Shell Programming and Scripting

using mv command for moving multiple files in a folder

Hi, I have a requirement where I need to move Bunch of folders containing multiple files to another archive location. i want to use mv command .I am thinking when we use mv command to move directory does it create directory 1st and then move all the files ? e.g source... (4 Replies)
Discussion started by: rkmbcbs
4 Replies

8. UNIX for Advanced & Expert Users

Moving 1000s of files to another folder

Hi, I need to move 1000s of files from one folder to another. Actually there are 100K+ files. Source dir : source1 Target dir : target1 Now if try cp or mv commands I am getting an error message : Argument List too long. I tried to do it by the time the files are created in the... (1 Reply)
Discussion started by: unx100
1 Replies

9. Shell Programming and Scripting

Script for moving files from one folder to other

Hi I need to move last 1 year old files from one folder to another location on same server.How to write a shell script for the same? thanx Lalit (8 Replies)
Discussion started by: lalitkumar
8 Replies

10. Shell Programming and Scripting

Script for moving files from one folder to other

Hi All I need a shell script for move the 1 year old files from one folder to another folder with date and time. How to write a shell script for the same pls hepl me out. thanx in advance Thanx Lalit (2 Replies)
Discussion started by: lalitkumar
2 Replies
Login or Register to Ask a Question