Loop to move files in different directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop to move files in different directories
# 1  
Old 10-05-2010
Loop to move files in different directories

Hi,

I have various log files in different paths. e.g.
Code:
a/b/c/d/e/server.log
a/b/c/d/f/server.log
a/b/c/d/g/server.log
a/b/c/h/e/server.log
a/b/c/h/f/server.log
a/b/c/h/g/server.log
a/b/c/i/e/server.log
a/b/c/i/e/server.log
a/b/c/i/e/server.log

and above these have an archive folder e.g.
Code:
a/b/c/d/e/server.log
a/b/c/d/e/archive/
a/b/c/d/f/server.log
a/b/c/d/f/archive/
a/b/c/d/g/server.log
a/b/c/d/g/archive

How do i go about moving the server.log files into the respctive archive folder in the same directory structure. I could use a find statement along the lines of
Code:
find /a/b/c/ '*.log' -exec mv * /archive/ {} \:

??

Last edited by acc01; 10-06-2010 at 10:18 AM.. Reason: Code tags for listings too :)
# 2  
Old 10-05-2010
Code:
$ for d in /a/b/c/*/*/archive/
do
 mv ${d%/archive/}/server.log $d
done

# 3  
Old 10-06-2010
Code:
cd /a/b/c

find . -type f -name "*.log" |while read file
do
  mv $file ${file%/*}/archive
done

# 4  
Old 10-06-2010
forgot to mention this will be run on a daily cron job. I used the code below to run this process but it deletes all the .log files that are already in the archive folder. How do I get the .log files that are already in the archive folder to stay and move the rest of the .log files that are not in archive folder into archive?

Code:
cd /a/b/c

find . -type f -name "*.log" |while read file
do
  mv $file ${file%/*}/archive
done

Thanks
# 5  
Old 10-06-2010
BTW: I hardly ever cd, even in scripts, as I found it hindered my productivity and led to errors. The problem with cd is then, commands are not reusable. The cd is really a typing shorthand. Effectively, it is in competition with command recall. Absolute paths are not much of a burden if they are not typed. UNIX ksh life was more consistent, error-free, better if I used X cut/paste, command recall & editing: set -o vi/viraw w/export HISTSIZE=32767, even archiving .sh_history occasionally for my tools that recall history. I even wrote a vi wrapper so my xterm scroll history (also set real big) was not overwritten and the return is always zero (so results are not discarded). Since I never leave $HOME, I can use relative paths for all common things, some through my own sym-links, and absolute for things less frequent. All my history is rerunnable. If it needs cd, I do: (cd ... ; .... )

---------- Post updated at 11:20 AM ---------- Previous update was at 11:12 AM ----------

My solution moves one dir at a time, not one file, which might be a bit faster and lower overhead.

It also ensures the target is present. It does not check to see if the source files are present, but that is not worth scripting or easy to script cheaply.

If you get a huge dir, when someone allows too many files to expand the directory inode, the speed difference is very substantial. With my plan, you are going to traverse that directory:
  1. once in find to find the archive/,
  2. once in ksh to find the source files for the mv command line, and then
  3. once (for every file?) inside mv to find the archive\
  4. once for every file inside mv, but just far enough through the directory to find that file and overwrite that part of the directory.
# 6  
Old 10-06-2010
Quote:
Originally Posted by acc01
forgot to mention this will be run on a daily cron job. I used the code below to run this process but it deletes all the .log files that are already in the archive folder. How do I get the .log files that are already in the archive folder to stay and move the rest of the .log files that are not in archive folder into archive?

Code:
cd /a/b/c

find . -type f -name "*.log" |while read file
do
  mv $file ${file%/*}/archive
done

Thanks
you can exclude the archive folder.

Code:
find . -type f -name "*.log" |grep -v archive |while read file
do
  mv $file ${file%/*}/archive
done

# 7  
Old 10-06-2010
When moving logs (hopefully unattached: not open long term), time stamps in the file names are nice, like log_until_$(date '+%Y''%m''%d_%H''%M''%S'). Do "sleep 1" after each so all logs are uniquely named.

Why the ''? things like %M% are SCCS metastrings, so this means they can be checked in.

Also, cron likes % as a metacharacter so persistently that I wrote a date replacement wrapper script date_cron using ~ for %, just so cron lines could use dated log files from the very start!

No need for find when all the files you want to move are at the same depth, just glob:

Code:
for f in a/b/c/*/*/server.log
do
 mv $f ${f%/server.log}/archive/log_until_$(date '+%Y''%m''%d_%H''%M''%S')
 sleep 1
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Move several files into specific directories with a loop

Hello, I'm a first time poster looking for help in scripting a task in my daily routine. I am new in unix but i am attracted to its use as a mac user. Bear with me... I have several files (20) that I manually drag via the mouse into several named directories over a network. I've used rsync... (14 Replies)
Discussion started by: SonnyClark
14 Replies

2. Shell Programming and Scripting

Need BASH Script Help to Move Files While Creating Directories

I've got this script to loop through all folders and move files that are more than 2 years old. I'm using the install command because it creates the necessary directories on the destination path and then I remove the source. I'd like to change the script to use the mv command since it is much... (4 Replies)
Discussion started by: consultant
4 Replies

3. UNIX for Dummies Questions & Answers

Move multipe files to corresponding directories

Hi, In a parent directory there are several files in the form IDENTIFIER1x IDENTIFIER1.yyy IDENTIFIER1_Z, etc IDENTIFIER2x IDENTIFIER2.yyy IDENTIFIER2_Z, etc IDENTIFIER3x IDENTIFIER3.yyy, IDENTIFIER3_Z, etcIn the same parent directory there are corresponding directories named... (7 Replies)
Discussion started by: spirospap
7 Replies

4. Shell Programming and Scripting

Recursively move directories along with files/specific files

I would like to transfer all files ending with .log from /tmp and to /tmp/archive (using find ) The directory structure looks like :- /tmp a.log b.log c.log /abcd d.log e.log When I tried the following command , it movies all the log files... (8 Replies)
Discussion started by: frintocf
8 Replies

5. OS X (Apple)

Batch file to move video files and retain sub-directories

I have just purchased my first ever Apple computer - and am therefore new to UNIX also. I would like to create a simple "batch file" (apologies if this is the wrong terminology) to do the following: When I plug my camera into the MAC it automatically downloads photos and videos into a new... (1 Reply)
Discussion started by: mm0mss
1 Replies

6. Shell Programming and Scripting

Help with command to Move files by X number to seperate directories

Hello, I need help finding a script that will allow me to move files from one directory to another directory 10k files at a time. I have a directory that has 100 K files in it. I need to have those 100k files broken apart to separate directories each with 10k files in them. Here is the... (8 Replies)
Discussion started by: Geo_Bean
8 Replies

7. Shell Programming and Scripting

want to move files in a dir into different directories based on the filename

I want to move the files in a dir to different dirs based on their file names. Ex: i have 4 different files with name - CTS_NONE_10476031_MRL_PFT20081215a.txt CTS_NONE_10633009_MRL_PFT20091020a.txt CTS_NONE_10345673_MRL_PFT20081215a.txt CTS_NONE_10872456_MRL_PFT20091020a.txt and the 1st... (4 Replies)
Discussion started by: Sriranga
4 Replies

8. UNIX for Dummies Questions & Answers

want to move files in a dir into different directories based on the filename

I want to move the files in a dir to different dirs based on their file names. Ex: i have 4 different files with name - CTS_NONE_10476031_MRL_PFT20081215a.txt CTS_NONE_10633009_MRL_PFT20091020a.txt CTS_NONE_10345673_MRL_PFT20081215a.txt CTS_NONE_10872456_MRL_PFT20091020a.txt and the 1st... (2 Replies)
Discussion started by: Sriranga
2 Replies

9. Shell Programming and Scripting

How to loop through directories to touch files

Hi, Please help me on this. Suppose i have the following directory structure. /app/data /app/data/eng /app/data/med /app/data/bsc each of the directories data,data/eng,data/med,data/bsc holds files with date extension like a.20081230 b.20081230 and so on I need a script to loop... (9 Replies)
Discussion started by: sussane
9 Replies

10. Shell Programming and Scripting

grep'ing for specific directories, and using the output to move files

Hello, this is probably another really simple tasks for most of you gurus, however I am trying to make a script which takes an input, greps a specific file for that input, prints back to screen the results (which are directory names) and then be able to use the directory names to move files.... (1 Reply)
Discussion started by: JayC89
1 Replies
Login or Register to Ask a Question