I have two files file1.txt
, flie2.txt
in a directory X
. I want to copy those files to my current working directory which is far away from the directory X
.
so I end up with a command like:
cp ~/Desktop/dir/dir/dir/dir/file1.txt ~/Desktop/dir/dir/dir/dir/file2.txt .
Is there anyway to use the directory path once for both file, some kind of a shortcut ?
I am sure there is, Ubuntu never failed my laziness 😀
Best Answer
Use
[1-2]
to indicate the files, this usesbash
's range expansion feature :Here the range
[1-2]
will expand sofile[1-2].txt
will expand tofile1.txt
andfile2.txt
.Note that if you have multiple files like this with various number of numeric digits among their names :
In this case use the
extglob
feature ofbash
to enable extended pattern matching:The extglob pattern
+([0-9])
will match one or more digits betweenfile
and.txt
in the file names.Also note that it does not depend on any prefix or suffix, for example if you have files having names:
These can be matched (and copied) too by :
Check
man bash
to get more idea on this.