Han Zhengzu
Han Zhengzu

Reputation: 3842

How to select several files by their names in shell

For example, there are many files in the same path named like:

FILE-2013-01-02.csv, FILE-2013-01-03.csv,......FILE-2013-12-31.csv   

I want to select several files and copy them into other path.

Simply, to select files containing the information for January, I use cp -r FILE-2013-01* ./other/path.

If I want to select all the files in Month 1, 3, 5, 7 with one sentence in shell, what should I do?

Upvotes: 0

Views: 145

Answers (4)

Zumo de Vidrio
Zumo de Vidrio

Reputation: 2091

Use it as follows:

cp -r FILE-2013-0{1,3,5}* your_path

Upvotes: 1

lw0v0wl
lw0v0wl

Reputation: 674

cp -r FILE-2013-0[1357]-[0-3][0-9].csv /otherpath/

If you want to mach 2013-11 and 2013-02 but no 2013-12 or 2013-01 you could use grep -E -w to get more complex regexp options: 2013-(1[01]|0[2357])

ls | grep -E -w '^FILE-2013-(1[01]|0[2357])-[0-3][0-9].csv$' | xargs -i cp {} /otherpath/

I prefer find to achieve this.

find ./ -type f -name "FILE-2013-0[1357]-[0-3][0-9].csv" -exec cp -r {} /otherpath/ \;

Upvotes: 0

signjing
signjing

Reputation: 98

You can use this:

cp -r FILE-2013-0[1357]-*.csv your_path

Upvotes: 2

yoones
yoones

Reputation: 2474

This should do it:

cp -r FILE-2013-0{1,3,5,7}* ./other/path

Upvotes: 2

Related Questions