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
noob help needed ace_face Linux 1 04-20-2008 08:23 PM
unix noob help with awk? AnnaLynn Shell Programming and Scripting 11 04-01-2008 09:29 AM
Unix Noob, wat do i need etc skylin3fr3ak UNIX for Dummies Questions & Answers 2 02-24-2008 11:17 AM
complete noob avdrummerboy UNIX for Dummies Questions & Answers 3 12-04-2006 12:25 PM
I am a unix noob alt+f4 UNIX for Dummies Questions & Answers 4 04-28-2006 09:01 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-30-2007
Sax Sax is offline
Registered User
  
 

Join Date: May 2007
Posts: 8
Question Shell Noob

Hi all,

I am trying to write a shell script that will move files from one directory to another, the only thing is I want to to check loads of different source directory and move the files to loads of different directories.

I am totally new to shell scripts but not to UNIX (although I would still class myself as a newbie).

Any advice would be welcome, as I am sat looking at a rather empty page in vi and goggling like crazy but not coming up with much.
  #2 (permalink)  
Old 05-30-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
You are kinda vague. If you need to move a whole tree try something like this:
Code:
cd /path/to/upper/level/of/source
tar -cvf - $(find . -print -type f) | (cd ../destination ; tar -xvf -)
find gets all of the files in the source tree, feeds the file to tar, the tar on the other end of the pipe then parks the file in a new directory tree with the same subdirectories. This is not deleting anything. This is a VERY general solution -
  #3 (permalink)  
Old 05-30-2007
Sax Sax is offline
Registered User
  
 

Join Date: May 2007
Posts: 8
Ok, being new to this I have drawn a pic to help me explain......



ok hopefully this will help me explain what I'm trying to do.

I did ls | wc -l on /user and that gave 392, which sounds about right (give or take 15 random other directories and files that are in there).

So I am trying to move files from one private folder (under xxxx.old) to the other private folder, I need to do this somewhere in the region of 350 times on different folders.

Apologies for my naff explanation but hopefully my wonderful picture will give you a clue what I'm on about
  #4 (permalink)  
Old 05-30-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Sax,
See if this works for you.
Note that I commented the "cp" (copy) statement so that you can
make sure it does what you want first, before running it.
Code:
for mNewDir in `find /user/ -type d -name 'accu*'`
do
  mNewBase=`basename ${mNewDir}`
  mNewPriv=${mNewDir}"/private/"
  mOldPriv="/user/dotolds/"${mNewBase}".old/private/"
  echo "Now copying from "${mOldPriv}" to "${mNewPriv}
  #####cp ${mOldPriv} ${mNewPriv}
done

Last edited by Shell_Life; 05-30-2007 at 03:53 PM..
  #5 (permalink)  
Old 05-31-2007
Sax Sax is offline
Registered User
  
 

Join Date: May 2007
Posts: 8
Cheers, looks like just what i'm after, however I am getting a syntax error on line 5 (syntax error at line 5: `mNewPriv=$' unexpected), the only thing I have changed is the find (see below).

I have been through I a couple of time to check what I have typed in vi matches your solution, and I am sure I have typed it correctly.

Code:
for mNewDir in `find /user/ -type d -name 'accu*.old'`
do
  mNewBase=`basename ${mNewDir}`
  mNewPriv=${mNewDir}"/private/"
  mOldPriv="/user/dotolds/"${mNewBase}".old/private/"
  echo "Now copying from "${mOldPriv}" to "${mNewPriv}
  #####cp ${mOldPriv} ${mNewPriv}
done
  #6 (permalink)  
Old 05-31-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Sax,
The "find" statement is to loop through all the "accuNNN" directories,
NOT the "accu*old".
Display the "mNewDir" right after the "do" to see what value you are getting:
Code:
echo "mNewDir = <"${mNewDir}">"
Also, I wrote the shell in ksh -- are you using the same?
  #7 (permalink)  
Old 05-31-2007
Sax Sax is offline
Registered User
  
 

Join Date: May 2007
Posts: 8
I think I see how it's working now.
I changed the find back, and changed the script to ksh. I also added the line above.
When I run it I don't get any errors, but then I don't get anything else ? I left the copy command commented out and ran it, but I didn't get any output, I thought I would at least see the echo lines
Sponsored Links
Closed Thread

Bookmarks

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:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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