Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy multiple files from one directory to another from Linux shell [closed]

Tags:

linux

bash

shell

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 ;-)

like image 518
ART Avatar asked Jun 13 '14 13:06

ART


People also ask

How do I copy multiple files from one directory to another in Linux?

To copy multiple files you can use wildcards (cp *. extension) having same pattern. Syntax: cp *.

How do I copy files from one directory to another in Shell?

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 .

How do you copy multiple files from one folder to another?

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.


2 Answers

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

tab completion with zsh...

enter image description here

like image 146
Kent Avatar answered Oct 14 '22 11:10

Kent


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.

like image 45
Barmar Avatar answered Oct 14 '22 11:10

Barmar