edited to better explain question.

so I want to scan a directory, look for subdirectories containing --import-me at the end of the directory name, move to a new directory with all files contained, just rename the dir without the --import-me string

cd $some-dir
for f in `find . -name "*--import-me" `
echo mv $f "some-other-dir"/"$rename"

I get

mv ./iZombie /some-other-dir/./iZombie
mv (2015)--import-me /some-other-dir/(2015)

I expect it to give me

mv ./some-other-dir/iZombie (2015)
  • 1
    You don't need echo | sed, just ${file%--import-me} is enough. You also have typos. Other than that, you didn't really say what's your problem. You only made a statement.
    – 4ae1e1
    Nov 11, 2015 at 3:29
  • 1
    Define "does not work". That's the most important part of the question.
    – 4ae1e1
    Nov 11, 2015 at 3:56

2 Answers 2


It seems from your example that the *--import-me directories exist only in the current directory, if so you can use this one-liner:

find . -type d -name "*--import-me" -exec sh -c 'mv $0 some-dir/${0%--import-me}' {} \;

Otherwise if you have nesting directories you must use this one, note that if there are / in the target directory (some-dir) you will have to escape them \/:

find . -type d -name "*--import-me" -exec sh -c 'mv $0 `echo $0| sed "s/.*\/\(.*\)--import-me/some-dir\/\1/g"`' {} \;
  • something like this could work... but still doesn't mv "$dldDir"/*_import-me "$libDir" mv "$libDir"/*_import-me "$libDir"/${0%_import-me} Nov 11, 2015 at 16:53

so I can't seem to get this to work, the filename always gets split at spaces, so it tries to mv two separate dirs. this is running from a script, ideally would be to find directories containing "*_import-me", rename and move in one command, but now I am trying to move dirs (from $dldDir to $libDir) containing _import-me, then rename dirs in libDir (removing the string _import-me)

mv "$dldDir"/*_import-me "$libDir"
cd $libDir
find . -type d -name "*_import-me" -exec sh -c 'mv $0 `echo $0| sed "s/.*\/\(.*\)_import-me/"$libDir"\/\1/g"`' {} \;

So, the first mv works, and a ls $dldDir gives me

~# ls -1 $dldDir
Brother Bear 2 (2006)_import-me
Terminator Salvation (2009)_import-me

but the second mv(removing _import-me in $libDir) I get

mv: target ‘(2009)_import-me’ is not a directory
mv: target ‘(2006)_import-me’ is not a directory

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.