Help with Script to Move Directories


 
Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers Help with Script to Move Directories
# 1  
Old 03-15-2011
Help with Script to Move Directories

HTML Code:
Hi
I am after a simple script to move folders/files from one directory into another directory on the same server. I want to run a cron so this can run at midnight.
Issue is there will not always be data in the source folder.
This script works fine but it errors if nothing exists in the source directory


#!/bin/sh
#Move Archive#


SOURCE_DIR=/raid/ERIC/ARCHIVING_ONLY/
BACKUP_DIR=/raid/ARCHIVE/TO_ARCHIVE/


cd $SOURCE_DIR
mv -f * $BACKUP_DIR/
cd $TMP_DIR


Can this be added to script to ignore if data is not there
thanks
Treds
# 2  
Old 03-15-2011
Code:
$ sh -c 'if [ "`echo *`" = "*" ]
then
 echo true
fi'
$ mkdir emptyd
$ sh -c 'cd emptyd;if [ "`echo *`" = "*" ]
then
 echo true
fi'
true
$

# 3  
Old 03-21-2011
thanks

HTML Code:
thanks so would i run it like this?


#!/bin/sh
#Move Archive#


SOURCE_DIR=/raid/ERIC/ARCHIVING_ONLY/
BACKUP_DIR=/raid/ARCHIVE/TO_ARCHIVE/


cd $SOURCE_DIR
mv -f * $BACKUP_DIR/
cd $TMP_DIR

$ sh -c 'if [ "`echo *`" = "*" ]
then
 echo true
fi'
$ mkdir emptyd
$ sh -c 'cd emptyd;if [ "`echo *`" = "*" ]
then
 echo true
fi'
true
$
# 4  
Old 03-22-2011
No, my bit of shell scripting was intended to be tutorial. This is an example of how to test a directory for empty or not empty state, where I make a new directory so it is implicitly empty. I ran the bits in sh -c '...' as my login shell is ksh.

You would cd to the source, test it, and if not empty, mv.
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. Shell Programming and Scripting

Move directories in script.sh doesn't work

Dear All, I would like move some directories in another location. Basically, my ls -lis drwxr-xr-x 3 XXXXXXXXXXXXXXXXXXXX 4096 Feb 24 02:18 data.N701_N502.ABCDE -rw-r--r-- 1 XXXXXXXXXXXXXXXXXXXX 185865797 Feb 23 11:27 data.N701_N502.ABCDE_file1 -rw-r--r-- 1 XXXXXXXXXXXXXXXXXXXX... (2 Replies)
Discussion started by: giuliangiuseppe
2 Replies

4. 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

5. UNIX for Dummies Questions & Answers

Using tar to move directories

I have figured out how to create a tar file that holds all the files in a particular directory. The plan is to move the tar to a new system via FTP so that we can test the new system with our files and libraries. What I can't figure out is how to unzip the tar file; I keep getting messages that... (8 Replies)
Discussion started by: KathyB148
8 Replies

6. Shell Programming and Scripting

move directories up one level

hi , could you help me with shell scripting in a shell script i have these commands a=`ls -R $dir | grep ./ ` cp -R ./$a/* ./$output/ with the first command i have all the directories with the second command i want to copy them in a new directory something like this... (2 Replies)
Discussion started by: faethon
2 Replies

7. Shell Programming and Scripting

Loop to move files in different directories

Hi, I have various log files in different paths. e.g. 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... (6 Replies)
Discussion started by: acc01
6 Replies

8. 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

9. 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

10. UNIX for Dummies Questions & Answers

Compare directories then move similar ones

I would like to know how to compare a listing of directories that begin with the same four numbers ie. /1234cat /1234tree /1234fish and move all these directories into one directory Thanks in advance (2 Replies)
Discussion started by: tgibson2
2 Replies
Login or Register to Ask a Question