0

I've found many solutions for this problem already, but all them copying and after removing the files. I need a single command who move or copy and remove file by file.

I'm not too deep in command line so I could manage this below but I'm not sure how to remove after copy.

find . -name "*.extension" -exec cp --parent {} ../NewFolder/ \;

Using OSX. Thanks.

3
  • 1
    instead of just copying in your exec, do two things with "cp ..... && rm {} \;" – djsmiley2kStaysInside Mar 24 '17 at 17:23
  • I'm presuming you can't just use mv.... – djsmiley2kStaysInside Mar 24 '17 at 17:24
  • 1
    You're right! Post it as an answer pls! – Marcelo Filho Mar 24 '17 at 17:25
1

instead of just copying in your exec, do two things with "cp ..... && rm {} \;"

&& in bash means 'Run this command next, only if the previous command exited successfully (with a error code of 0)

2
1

To mimic cp --parents, you need to use mkdir and mv together.

find . -name "*.extension" -exec sh -c 'dir=../NewFolder/$(dirname "{}"); mkdir -p "$dir" && mv -v "{}" "$dir"' \;

Verbose console cut'n'paste:

$ tree source
source
└── a
    ├── b
    │   ├── b.extension
    │   ├── c
    │   │   └── c.extension
    │   └── d
    └── e
        └── f
            └── f.extension

6 directories, 3 files
$ tree dest
dest

0 directories, 0 files
$ cd source
$ find . -name "*.extension" -exec sh -c 'dir=../dest/$(dirname "{}"); mkdir -p "$dir" && mv -v "{}" "$dir"' \;
'./a/e/f/f.extension' -> '../dest/./a/e/f/f.extension'
'./a/b/c/c.extension' -> '../dest/./a/b/c/c.extension'
'./a/b/b.extension' -> '../dest/./a/b/b.extension'
$ cd ..
$ tree source
source
└── a
    ├── b
    │   ├── c
    │   └── d
    └── e
        └── f

6 directories, 0 files
$ tree dest
dest
└── a
    ├── b
    │   ├── b.extension
    │   └── c
    │       └── c.extension
    └── e
        └── f
            └── f.extension

5 directories, 3 files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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