Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This is for a java project, but the same concept can be applied more generally:

Basically, I have a projects with all *.java files located in some sub directory of src. Now I want to grab all directories with the name test in that directory tree and move them into a new directory called tests, e.g.:

src->com->a1
          -> A.java
          -> B.java
          -> test
              -> test1.java
              -> test2.java

to

src->com->a1
          -> A.java
          -> B.java
tests->com->a1->test
                -> test1.java
                -> test2.java

How would I best do that?

share|improve this question
    
Does it need to create the parent directories tests/com and tests/com/a1 for you? –  gertvdijk Dec 17 '12 at 16:28
    
not tests/ but the others, yes. That's why said it has to keep directory structure - that is, it has to create them if they aren't already there –  Cubic Dec 17 '12 at 16:36
    
are there other files, or just the *.java ones? –  philshem Dec 17 '12 at 17:10
    
is there some reason that you can't just drag and drop in the file browser? –  ImaginaryRobots Dec 17 '12 at 18:11
    
@ImaginaryRobots Other than that that won't do what I asked? No. –  Cubic Dec 17 '12 at 18:46
add comment

2 Answers

Please note that the meanings of move mv and copy cp are distinct and well defined for terminal usage. mv means that the files will no longer exist in the old location, while cp means that a duplicate is made in the new location.

Simple solution (notice that I use mv, but you may want to use cp first and then remove the old files once the copying is what you want):

cp src/com/a1/test/test*.java tests/com/a1/test/

More elegant solution (this one moves the files, not copies):

cd src/com/
find . -iname "test*.java" -type d -exec mv {} ../../tests/com/ \;

If the second solution doesn't work for your specific test case, we can iterate...

share|improve this answer
    
This doesn't do what I asked. I need to move ALL test folders from ALL sub folders to tests, keeping the directory structure. –  Cubic Dec 17 '12 at 18:47
    
In order to find all the files in "test" folders, run this command: find . -type d -name "test" -exec ls -l {} \; Use the type -d flag after the dot to search only directories and the type -f flag for files. You can figure out how to move the files based on the above answer. –  philshem Dec 17 '12 at 19:26
add comment

You'll need to write a shell script for this task. I'll give it a try and comment the lines.

The first line here is just a variable where you put the absolute directory of your src tree.

SRCDIR=/dir/where/src/lives

The next line executes find. The command will put every directory path with the name test into the variable TESTDIRS.

TESTDIRS=$(find $SRCDIR -type d -name test)

Now you iterate over all found test directories

for dir in $TESTDIRS; do

This takes the actual path and removes everything including src. So you get the structure for creating the new path.

  NEWDIR=${dir##*/src}

Now create the new directories.

  mkdir -p tests/$NEWDIR

Move the old test directory to the newly created place.

  mv $dir tests/$NEWDIR

end the iteration

done

As far as I understand you this should what you want. But please test it first. There might be some caveats and running the script as is might lead to data loss.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.