August 1, 2023

Move (cp) all files found with the command find

$ find /home/username/Download/ -mtime -3 -print -exec cp {} /media/username/usb-stick/ \;

will copy files created or modified within the last three days in the folder /Download to a USB stick that is mounted.

The beginning of this command is the usual find command, with the option -mtime. We add the option -print so the shell shows what’s being copied. Then comes -exec which runs the command cp on the selected files. The curly brackets i.e. {} come along with usage of -exec and are replaced by the current file name being processed. The semicolon i.e. ; tells -exec that it is the end of the command. Backslash i.e. \ is just here to escaped ;.

Note: -exec executes the command on each find (not as a batch).

To learn more about the option -exec, run $ man find.

To learn more about the option -mtime, check out: https://yctct.com/find-mtime

The utility find was published in 1971 and is copyleft-licensed.

Other wikis:


personal computing command-line interface (cli) gnu linux trisquel shell literacy office applications wiki find

No affiliate links, no analytics, no tracking, no cookies. This work © 2016-2024 by yctct is licensed under CC BY-ND 4.0 .   about me   contact me   all entries & tags   FAQ   GPG public key

GPG fingerprint: 2E0F FB60 7FEF 11D0 FB45 4DDC E979 E52A 7036 7A88