nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

Need help writing simple copy script

This is a discussion on Need help writing simple copy script within the Shell scripting forums, part of the Development/Scripting category; I have a situation in which i have many directories with one file in each of them. I would like ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 08-25-2006, 11:35 PM
Junior Member
 
Join Date: Aug 2006
Posts: 3
Rep Power: 0
z3cka
Default Need help writing simple copy script

I have a situation in which i have many directories with one file in each of them. I would like to write a script that would go into each directory and copy the file in that directory one level up.

For example, i would like the *.txt files here:

/startingDirectory/
-->dirA/
------>fileA.txt
-->dirB/
------>fileB.txt

To be copied or moved to yeild:

/startingDirectory/
-->dirA/
-->dirB/
-->fileB.txt
-->fileA.txt

*** the *.txt files can be copied or moved i don't have a preference.

I would like to do this in a linux bash shell. The names of the files should be arbitrary to the scripts. It should be able to just move to each directory recursively.

Thank you in advance, please let me know if i was not specific enough.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-26-2006, 05:05 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Ubuntu
Posts: 1,036
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

You need to use while loop to read file system:
Code:
ls | while read t; do echo $t; done
If directory found inside this loop will go to that dir and scan for file. If there is only one file just copy it.


Code:
#!/bin/bash
dirtreeloop()
{

  ls $1 | while read file
  do
   if [ -d "$1/$file" ]; then # if it is dir
      echo "Entering $1/$file ..."
      dirtreeloop "$1/$file"
    else
      # see if only one file exists
       count=$(ls -l $1 | egrep -v  '\.|\.\.|^total' | wc -l)
       # if so copy it to upper dir
       [ $count -eq 1 ] && cp $1/$file .. || :
       count=0
    fi
  done
}
dirtreeloop "$1"
Save and run script as follows:
Code:
chmod +x script.sh
./script.sh .
./script.sh /path/to/dir
Test it and let me know..
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #3 (permalink)  
Old 08-26-2006, 09:30 AM
Junior Member
 
Join Date: Aug 2006
Posts: 3
Rep Power: 0
z3cka
Default a good start

Thank you nixcraft, this is a good start.

first of all the script works!!! But, after about an hour of testing, i have came apon 2 problems that i think with a little tweeking we can solve.

1) i need the code to work with directories with spaces in the directory name.
2) the script copies the files to the target when there is 1 directory in the level it is on...

example:


in special_cp.jpg: fileA2.som and fileA.som will be copied to the target directory, while fileB.som and fileC.some are not copied. *dummyDir is empty by the way, don't think that matters though*

strange. We are close. I thank you for the quick response.
Reply With Quote
  #4 (permalink)  
Old 08-27-2006, 10:54 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Ubuntu
Posts: 1,036
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Quote:
1) i need the code to work with directories with spaces in the directory name.
Put $1 (i.e. dir name) into double quote so that shell recognize it as a single dir name with spaces here is a script:
Code:
#!/bin/bash
dirtreeloop()
{

  ls "$1" | while read file
  do
   if [ -d "$1/$file" ]; then # if it is dir
      echo "Entering $1/$file ..."
      dirtreeloop "$1/$file"
    else
      # see if only one file exists
       count=$(ls -l "$1" | egrep -v  '\.|\.\.|^total' | wc -l)
       # if so copy it to upper dir
       [ $count -eq 1 ] && echo cp $1/$file .. || :
       count=0
    fi
  done
}
dirtreeloop "$1"
Quote:
2) the script copies the files to the target when there is 1 directory in the level it is on...
I am sorry but I am not getting your point can you explain more...
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #5 (permalink)  
Old 08-27-2006, 03:29 PM
Junior Member
 
Join Date: Aug 2006
Posts: 3
Rep Power: 0
z3cka
Default

thanks for the fix to #1.

#2 is hard to explain, but i will try.

the script only copies the desired files when there is one directory in the same level of the desired files...

ie. in the example image of dirA. "dummyDir" exists in dirA therefore fileA.som and fileA2.som are copied to the target directory while fileB.som and fileC.som are skipped (and not copied) because they are do not have a directory on that level.

At least this is why i THINK that they arn't being copied, i could be wrong and there could be a different issue.

if this doesn't help, i will post a before and after script run tomorrow to help.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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

Similar Threads

Thread Thread Starter Forum Replies Last Post
Simple ipcalc perl script unixfoo Shell scripting 0 01-24-2008 07:59 PM
writing a shell script to find out my shell name jaymob123 Shell scripting 1 10-08-2007 01:36 AM
need help in writing backup script vishaltitre Shell scripting 4 05-23-2007 11:07 PM
How to copy . files from current directory ricc Linux software 1 06-10-2006 10:23 PM
DVD copy howto tom Getting started tutorials 1 01-11-2006 11:43 PM


All times are GMT +5.5. The time now is 06:43 PM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36