Junba Tester
Junba Tester

Reputation: 851

bash script to copy specific set of files

I have a set of files named by numbers from 1 to 1000 .csv, I want to copy files whose name starts with 1 with 2 or less digits(eg. 1,10,11,12,etc). how can this be done?

Upvotes: 1

Views: 1464

Answers (2)

djadmin
djadmin

Reputation: 1760

You can do it easily :

cp 1[0-9].csv /home
cp 1.csv /home

Save this as file.sh and then run as

$bash file.sh

This will copy all files to home Directory

Upvotes: 2

Thomas
Thomas

Reputation: 181745

The correct way that should always work:

find -regex '\./1[0-9]?\.csv' -exec cp {} destination \;

As find uses the full path, we have to match a ./ at the beginning. Then follows a literal 1, then optionally (note the ?) a character from the range 0-9, and finally the extension. Any .s must be escaped as \..

For each matching file, find will execute the command given by -exec, up to the \;, substituting {} by the filename.

To check that it catches the right files, do just:

find -regex '\./1[0-9]?\.csv'

A simpler way, that works if there's no other clutter in your directory:

cp 1.csv 1?.csv destination

Upvotes: 2

Related Questions