2

I'm wondering if anybody knows how to find a pattern and then move it to a different location.

For example, I have many files named:

odbc.ini_20110630
odbc.ini_20110639
odbc.ini_20110643
etc...

I want to search the pattern of just odbc.ini and move all of them to a different folder.

I'm not too familiar with how to execute two commands at one time (piping).

jasonwryan
  • 73,126
mkrouse
  • 939

3 Answers3

5

You can use

find . -name "odbc.ini*" -exec mv {} destination \;

This is assuming that your files are in the directory hierarchy starting at current directory ..

unxnut
  • 6,008
  • 1
    Just out of curiosity - does this destroy all the files if the directory does not already exist? – InquilineKea Oct 20 '13 at 21:20
  • You mean the destination directory? If that happens, I am afraid the answer is yes, except for the last moved file which will be named destination. – unxnut Oct 21 '13 at 01:02
1

if the files are all in a single directory and you don't need any recursion:

shopt -s nullglob
mv odbc.ini* /new/directory/

if you need recursion:

find "${dir:-.}" -type f -name 'odbc.ini*' -exec mv {} /new/directory \;

Another approach could be with extglob but i'll leave that as an exercise for the reader =]

1

If all the files are in the same directory:

mv /path/to/source/odbc.ini* /path/to/destination

If you want to move files in subdirectories as well:

shopt -s globstar      # put this line in your ~/.bashrc
mv /path/to/source/**/obdc.ini* /path/to/destination