1

This question already has an answer here:

I would like to move all the folders contained in the folder "/example" named e.g. "*_jony" to a new directory call "/jony". I have try several way without success! It would be great to have some helps. Thanks!

marked as duplicate by DopeGhoti, Ramesh, jimmij, don_crissti, slm Jul 28 '15 at 21:35

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Are you looking for a command with which to do this? Or will a script do? – GreenMatt Jul 28 '15 at 19:23
1
mv path_to_example_dir/*_jony /jony
0

If this is something you need to do somewhat frequently, you will probably find it useful to have a script to do it. Following is a quickly assembled Bash script which can do the job:

#!/bin/sh
dir_str=$1
src_dir=$2
dest_dir=$3
ret_code=0
if [ ! -e $src_dir ] ; then
    echo "Could not find source directory $src_dir."
    let "ret_code += 1"
fi
if [ ! -e $dest_dir ] ; then
    echo "Could not find destination directory $dest_dir. (Use mkdir to create.)"
    let "ret_code += 2"
fi
if [ $ret_code -eq 0 ] ; then
    for dir_to_move in $src_dir/*$dir_str ; do
        echo "For '$dir_to_move':"
        cmd="mv $dir_to_move $dest_dir"
        echo "Executing: $cmd"
        $cmd
    done
fi
exit $ret_code

Use your favorite editor to create the script.

While it's not that advanced, it has some flexibility, in that you can specify the text in the directory names that are to be moved, and the source and destination directories. If you named the file holding the script "mv_dirs.sh", then to run it to achieve your stated goal, you can enter something like this:

sh mv_dirs.sh _jony /example /jony

To actually make this work, you'll need write permission in the root (/) directory, which normal users don't usually have.

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