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 ...
|
|||||||
| Register | FAQ | Members List | Calendar | Forgotten your password? | Mark Forums Read |
|
|||
|
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. |
| Sponsored Links | ||
|
|
|
|||
|
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. |
|
||||
|
Quote:
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:
|
|
|||
|
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. |
![]() |
| Bookmarks |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| 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 |