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 :D
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.I would use brace expansion for this kind of thing
I'm using History Expansion with Word Designators a lot.
!$
will designate the last argument of the preceding command.In most cases I take a look into that folder with
ls
first before coping.As you can see bash automatically replaced
!$
with the last argument of the preceding command.You can even use parts of that argument.
!$:h
(h for head) would designate the dirname of the last argument.!$:t
(t for tail) would designate the basename of the last argument.So, if you've read the file before with
less
you would doQuestion asked can be solved by using the following command too.
it will copy any files with the naming style file(something in between).txt