I'm trying to copy only the files from one directory (not including folders or any files in its subfolders) to another location using cp /media/d/folder1/* /home/userA/folder2/. It is copying the files alright but the problem is that there are a list of messages appear saying cp: omitting directory.... for all the folders located in folder1. Is there any other way to copy these folders without having this message appearing? Also another thing please, I'm asking the same thing if I wants to move (not copy), how this can be done? Thanks

  • 1
    If you are only annoyed by the messages you can redirect them by appending 2>/dev/null. (No need for complex commands and command pipes.)
    – Janis
    Commented Mar 16, 2015 at 2:44

3 Answers 3

find /media/d/folder1/ -maxdepth 1 -type f | xargs cp -t /home/userA/folder2

Part before the pipe character | finds the files in the given directory without attempting to find other files under any sub-directory of the given directory. Part after the pipe takes those files and copies them to the destination directory. You can change cp command with mv if you want to move files instead of copying.

  • 1
    I'd change this to find /media/d/folder1/ -maxdepth 1 -type f -print0 | xargs -0 cp -t /home/userA/folder2 ao that files with special characters (such as spaces) are also copied.
    – mdpc
    Commented Mar 16, 2015 at 2:01

A safer approach (that can deal with file names with spaces, newlines and other odd characters) is to use find itself and its -exec action:

   -exec command {} +
          This  variant  of the -exec action runs the specified command on
          the selected files, but the command line is built  by  appending
          each  selected file name at the end; the total number of invoca‐
          tions of the command will  be  much  less  than  the  number  of
          matched  files.   The command line is built in much the same way
          that xargs builds its command lines.  Only one instance of  `{}'
          is  allowed  within the command.  The command is executed in the
          starting directory.

So, you could do:

find /media/d/folder1/ -maxdepth 1 -type f -exec cp {} -t /home/userA/folder2

Note that this wil also copy hidden files.

cp -R dir1/* dir2

This will copy all contents (file as well as the sub-directory) from dir1 to dir2 without any errors.

You must log in to answer this question.

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