1

hope you can help me, I'm looking for a command to let me copy all files above 500kB in my directory one to directory 2..

Basically I have a lot of Images in directory1 and would like to copy all images larger than 500kB to my other folder directory2..

3
  • Do you want to copy, move, or make a link? You probably want to either move or make a (hard) link. Copying is rarely necessary. Commented Nov 26, 2012 at 19:34
  • Well, I want to copy, because thats what i want. My folder of images is made by foremost, so I want to keep the collection as a whole untill later...
    – Qw_freak
    Commented Nov 26, 2012 at 22:08
  • I notice that I am getting some votes down, why is this? Should i have elaborated that I had tried various things with xargs, which did'nt work before asking my question?
    – Qw_freak
    Commented Nov 27, 2012 at 7:38

2 Answers 2

6

You can use find thus:

find directory1 -type f -size +500k -exec cp -nv {} directory2/ \;
  • -v : verbose copy (you see what's copying)
  • -n : noclobber (might not be available to your cp version): do not overwrite an existing file (just in case).
5
  • Thanks man, it really helped, I had like a million images ranging from 1kB to 7mB, and 99% of those where >500kB... They where collected by foremost because i acidently deleted an image folder...
    – Qw_freak
    Commented Nov 26, 2012 at 22:10
  • Why the Backslash semicolon in the end?
    – Qw_freak
    Commented Nov 27, 2012 at 7:38
  • @Qw_freak because for bash, a semicolon is a separation character, so if not escaped, it would not be sent to find. But find needs to have a semicolon to know where the -exec option ends. We must hence escape it. Commented Nov 27, 2012 at 9:49
  • Use cp --parents to preserve the directory structure. Commented Jun 27, 2018 at 11:50
  • Use --maxdepth 1 to prevent a recursive find. It will only perform the find specifically on the target directory and not subdirectories. Commented Apr 24, 2021 at 14:46
2

This should do it:

find directory1 -type f -size +500k -exec cp {} directory2 \;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .