Ubuntu – Operating on multiple files in a directory

bashcommand line

Suppose I want to copy (or for that matter, perform any operation on) two files, dir1/dir2/foo.abc and dir1/dir2/bar.xyz, to the current directory. What I end up doing is this:

cp dir1/dir2/bar.xyz dir1/dir2/foo.abc .

Is there a less cumbersome way to do the same? I mean something like:

cp dir1/dir2/["bar.xyz", "foo.abc"] .

Best Answer

Yes - in fact very like that - but with braces rather than square brackets:

$ cp -v dir1/dir2/{bar.xyz,foo.abc} ./
'dir1/dir2/bar.xyz' -> './bar.xyz'
'dir1/dir2/foo.abc' -> './foo.abc'

See brace expansion.