I want to copy several files from one folder to another. How do I do it from the shell command prompt?
Consider that folder1
contains ten files (e.g. file1
, file2
, abc
, xyz
, etc.). I am currently doing the following in order to copy two files from one folder to another:
cp /home/ankur/folder/file1 /home/ankur/folder/file2 /home/ankur/dest
Typing the full path into the command line for both the files is annoying.
What comes to my mind is regex, but I don't quite know how to do it.
Any help will reduce my RSI ;-)
To copy multiple files you can use wildcards (cp *. extension) having same pattern. Syntax: cp *.
Copy a File ( cp ) You can also copy a specific file to a new directory using the command cp followed by the name of the file you want to copy and the name of the directory to where you want to copy the file (e.g. cp filename directory-name ). For example, you can copy grades. txt from the home directory to documents .
To do this, click and hold your left mouse button on the top-left portion of where you want to start highlighting. Next, drag the box until the last file or folder is highlighted. Once the files are selected, they can be copied, cut, or dragged to another window to move them.
I guess you are looking for brace expansion:
cp /home/ankur/folder/{file1,file2} /home/ankur/dest
take a look here, it would be helpful for you if you want to handle multiple files once :
http://www.tldp.org/LDP/abs/html/globbingref.html
Use wildcards:
cp /home/ankur/folder/* /home/ankur/dest
If you don't want to copy all the files, you can use braces to select files:
cp /home/ankur/folder/{file{1,2},xyz,abc} /home/ankur/dest
This will copy file1
, file2
, xyz
, and abc
.
You should read the sections of the bash
man page on Brace Expansion and Pathname Expansion for all the ways you can simplify this.
Another thing you can do is cd /home/ankur/folder
. Then you can type just the filenames rather than the full pathnames, and you can use filename completion by typing Tab.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With