Which command is used to copy all files having the string chap and any two characters after that to the progs directory?

Dani AI

Generated

asked how to copy every file whose name starts with chap and has exactly two characters after that into the progs directory. correctly pointed toward the manual, and noted that the normal file-copy utility with a glob will do the job. The practical form uses a shell wildcard that matches exactly two characters after chap, and a target directory.

Run these steps (create the destination if needed, then copy the matching files):

mkdir -p progs
cp chap?? progs/

Notes and cautions:

  • ? is a single-character wildcard; chap?? matches chap01, chapAB, but not chap1 or chapXYZ.
  • Do not quote the pattern (e.g., "chap??"), or the shell will not expand it. If there are no matches, some shells pass the literal pattern to the copy command and you’ll see an error; in bash you can use shopt -s nullglob to make a non-match expand to nothing before running the copy.
  • Filenames starting with a dot are not matched by plain globs. If you need recursive or more complex name tests, use find with a suitable -name pattern.
  • Always verify what will be copied first with echo chap?? or ls -d chap?? to avoid surprises.

For details on shell filename expansion and the copy utility, see the Bash filename expansion documentation and the coreutils copy manual:

Recommended Answers

All 3 Replies

$ apropos cp
cp (1)               - copy files and directories

rtfm!

: It is not very useful to suggest reading the manual on a command you dont yet know. The point of the question was to determine cp; not to ask how to use it.
Perhaps something along the lines of: "cp or mv will copy or move files, respectively. If you'd like more information on either you can run the command apropos. For example: apropos cp will give you information on how to use the cp command.

yes, I'd better tell him to stfw "how to copy files"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.