LoginSignup
3
4

More than 5 years have passed since last update.

findで見つかったファイルをリネームして同じディレクトリにコピーする

Last updated at Posted at 2016-06-03

ファイル名の一部を置換しつつ、同一ディレクトリ内にコピーする方法。

$ ls -al
total 0
drwxr-xr-x   5 goto  staff  170  6  3 13:07 .
drwxr-xr-x  18 goto  staff  612  6  3 13:06 ..
-rw-r--r--   1 goto  staff    0  6  3 13:07 foo_1.txt
-rw-r--r--   1 goto  staff    0  6  3 13:07 foo_2.txt
-rw-r--r--   1 goto  staff    0  6  3 13:07 foo_3.txt

$ find . -name "foo*" -exec bash -c 'cp $0 ${0/foo/bar}' {} \;

$ ls -al
total 0
drwxr-xr-x   8 goto  staff  272  6  3 13:08 .
drwxr-xr-x  18 goto  staff  612  6  3 13:06 ..
-rw-r--r--   1 goto  staff    0  6  3 13:08 bar_1.txt
-rw-r--r--   1 goto  staff    0  6  3 13:08 bar_2.txt
-rw-r--r--   1 goto  staff    0  6  3 13:08 bar_3.txt
-rw-r--r--   1 goto  staff    0  6  3 13:07 foo_1.txt
-rw-r--r--   1 goto  staff    0  6  3 13:07 foo_2.txt
-rw-r--r--   1 goto  staff    0  6  3 13:07 foo_3.txt

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4