2

How could I copy every regular files (not directories or any other type) from ~/foo that ends in a digit greater or equal than 3 to ~/bar? I tried with the find and cp commands, but I couldn't get it to work: find ~/foo -type f -regex '.*[!012]' -exec cp ~/bar.

Examples:

  • some.file12 wouldn't be copied because its last digit is 2, and 2 < 3.
  • somefile603 would be copied. 3 = 3.
  • foo3405 would be copied. 4 > 3.
  • file02.txt wouldn't be copied. It doesn't end in a digit.
  • 3
    can you show some example filenames? does "some.file12" end in a number greater than 3? Or do you only care about the last digit? – glenn jackman Jun 20 '17 at 16:49
  • Please post a filename example, otherwise your question isn't clear and we can't provide an answer – Sergiy Kolodyazhnyy Jun 20 '17 at 17:02
  • Example filenames added. I'm sorry for the ambiguous question. – user661429 Jun 20 '17 at 18:54
  • @Cytotlewo check my answer, let me know if you have any question ;) – Ravexina Jun 20 '17 at 19:35
3

It's going to work (Includes directories too):

cp ./*[3-9] /path/to/dst

with find (only files):

find ./src -iname "*[3-9]" -type f -exec cp {} path/to/dst/ \;

using regex (only files):

find ./src -regex "^.*[3-9]$" -exec cp {} path/to/dst/ \;
2
pdebski@PaDe:~/test$ ls -l
total 0
-rw-rw-r-- 1 pdebski pdebski 0 cze 20 18:42 a1
-rw-rw-r-- 1 pdebski pdebski 0 cze 20 18:42 a7
-rw-rw-r-- 1 pdebski pdebski 0 cze 20 18:45 a9
pdebski@PaDe:~/test$ ls -l ../test2
total 0
pdebski@PaDe:~/test$ find . -name '*[3456789]' -print -exec cp {} ../test2 \;
./a9
./a7
pdebski@PaDe:~/test$ ls -l ../test2
total 0
-rw-rw-r-- 1 pdebski pdebski 0 cze 20 18:51 a7
-rw-rw-r-- 1 pdebski pdebski 0 cze 20 18:51 a9
  • 1
    You can use [3-9] instead of [3456789]. – Ravexina Jun 20 '17 at 16:55
  • Sure @Ravexina. I realized just after posting. – Pawel Debski Jun 20 '17 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy