cp only files, skipping directories












3















Using cp to copy files from a directory that has an empty directory in it as well results in error return value, which fails my script.



Is there a way for it to just skip the directories without having error return value? I'd like error value to reflect any errors, but having an empty directory in there should be just skipped, without being reported as an error.



I could just add recursive flag -r but what if I just don't want the directory to be copied, but I do want any errors to be reported?










share|improve this question





























    3















    Using cp to copy files from a directory that has an empty directory in it as well results in error return value, which fails my script.



    Is there a way for it to just skip the directories without having error return value? I'd like error value to reflect any errors, but having an empty directory in there should be just skipped, without being reported as an error.



    I could just add recursive flag -r but what if I just don't want the directory to be copied, but I do want any errors to be reported?










    share|improve this question



























      3












      3








      3








      Using cp to copy files from a directory that has an empty directory in it as well results in error return value, which fails my script.



      Is there a way for it to just skip the directories without having error return value? I'd like error value to reflect any errors, but having an empty directory in there should be just skipped, without being reported as an error.



      I could just add recursive flag -r but what if I just don't want the directory to be copied, but I do want any errors to be reported?










      share|improve this question
















      Using cp to copy files from a directory that has an empty directory in it as well results in error return value, which fails my script.



      Is there a way for it to just skip the directories without having error return value? I'd like error value to reflect any errors, but having an empty directory in there should be just skipped, without being reported as an error.



      I could just add recursive flag -r but what if I just don't want the directory to be copied, but I do want any errors to be reported?







      linux ubuntu cp






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 18 '16 at 9:19







      eis

















      asked May 18 '16 at 9:11









      eiseis

      1,61611734




      1,61611734






















          2 Answers
          2






          active

          oldest

          votes


















          2














          I suggest you to use rsync instead of cp, if you can it has the option -m that will exclude empty folders, example:



          rsync -am <origin> <destination>


          Explanation of the command in explainshell.com



          I'm using the -a option because it does a lot of nice default action, but if you want you can also use the -r option, that will set the recursive mode only.



          [ADD comment]



          I do not know of a way to do this with cp, at least the man page does not show any option that will do that for you.
          You probably can do a more complex solution by using the command find to find list of files/directories to copy, and then pass it to cp, but that would be much more complicated than rsync. rsync is even pretty much standard in many linuxes






          share|improve this answer


























          • are you saying this can't be easily done with cp? I'd rather use that since it's more clear to immediately realize what it's doing, but of course if it cannot be used in that way I can look for other options.

            – eis
            May 18 '16 at 10:02



















          0














          cp indeed doesn't have an option to get only files, but as berserck said, you can use find together with cp. In fact, this is really straightforward:



          # Copy only files
          find /path/to/file -type f -execdir cp "{}" /dest/path ";"


          Or:



          # Copy everything except directories
          find /path/to/file -not -type d -execdir cp "{}" /dest/path ";"


          Explanation



          find is the tool that will look for the files you want. In the first option, we set -type f to return only regular files. In the second option, we set the -not -type d to get everything except directories. find executes the arguments passed to -execdir replacing {} with one result entry at a time and stops the command when ; is found (; should to be escaped ; or ";", or the shell might expand it).



          -execdir will run the command having the file's folder as its "working directory" and it is preferable instead of -exec, check below.



          The -type option documentation:




          -type c



          File is of type c:



          b: block (buffered) special



          c: character (unbuffered) special



          d: directory



          p: named pipe (FIFO)



          f: regular file



          l: symbolic link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken. If you want to search for symbolic links when -L is in effect, use -xtype.



          s: socket



          D: door (Solaris)




          The -not option documentation:




          ! expr



          True if expr is false. This character will also usually need protection from interpretation by the shell.



          -not expr



          Same as ! expr, but not POSIX compliant.




          The -execdir option documentation:




          -exec command ;



          Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of ';' is encountered. The string '{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command [...]. Both of these constructions might need to be escaped (with a '') or quoted to protect them from expansion by the shell. The specified command is run once for each matched file. The command is executed in the starting directory. There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.



          -execdir command ;



          Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find. This a much more secure method for invoking commands, as it avoids race conditions during resolution of the paths to the matched files. [...] If you use this option, you must ensure that your $PATH environment variable does not reference '.'; otherwise, an attacker can run any commands they like by leaving an appropriately-named file in a directory in which you will run -execdir. The same applies to having entries in $PATH which are empty or which are not absolute directory names.




          https://linux.die.net/man/1/find






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "3"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1078149%2fcp-only-files-skipping-directories%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            I suggest you to use rsync instead of cp, if you can it has the option -m that will exclude empty folders, example:



            rsync -am <origin> <destination>


            Explanation of the command in explainshell.com



            I'm using the -a option because it does a lot of nice default action, but if you want you can also use the -r option, that will set the recursive mode only.



            [ADD comment]



            I do not know of a way to do this with cp, at least the man page does not show any option that will do that for you.
            You probably can do a more complex solution by using the command find to find list of files/directories to copy, and then pass it to cp, but that would be much more complicated than rsync. rsync is even pretty much standard in many linuxes






            share|improve this answer


























            • are you saying this can't be easily done with cp? I'd rather use that since it's more clear to immediately realize what it's doing, but of course if it cannot be used in that way I can look for other options.

              – eis
              May 18 '16 at 10:02
















            2














            I suggest you to use rsync instead of cp, if you can it has the option -m that will exclude empty folders, example:



            rsync -am <origin> <destination>


            Explanation of the command in explainshell.com



            I'm using the -a option because it does a lot of nice default action, but if you want you can also use the -r option, that will set the recursive mode only.



            [ADD comment]



            I do not know of a way to do this with cp, at least the man page does not show any option that will do that for you.
            You probably can do a more complex solution by using the command find to find list of files/directories to copy, and then pass it to cp, but that would be much more complicated than rsync. rsync is even pretty much standard in many linuxes






            share|improve this answer


























            • are you saying this can't be easily done with cp? I'd rather use that since it's more clear to immediately realize what it's doing, but of course if it cannot be used in that way I can look for other options.

              – eis
              May 18 '16 at 10:02














            2












            2








            2







            I suggest you to use rsync instead of cp, if you can it has the option -m that will exclude empty folders, example:



            rsync -am <origin> <destination>


            Explanation of the command in explainshell.com



            I'm using the -a option because it does a lot of nice default action, but if you want you can also use the -r option, that will set the recursive mode only.



            [ADD comment]



            I do not know of a way to do this with cp, at least the man page does not show any option that will do that for you.
            You probably can do a more complex solution by using the command find to find list of files/directories to copy, and then pass it to cp, but that would be much more complicated than rsync. rsync is even pretty much standard in many linuxes






            share|improve this answer















            I suggest you to use rsync instead of cp, if you can it has the option -m that will exclude empty folders, example:



            rsync -am <origin> <destination>


            Explanation of the command in explainshell.com



            I'm using the -a option because it does a lot of nice default action, but if you want you can also use the -r option, that will set the recursive mode only.



            [ADD comment]



            I do not know of a way to do this with cp, at least the man page does not show any option that will do that for you.
            You probably can do a more complex solution by using the command find to find list of files/directories to copy, and then pass it to cp, but that would be much more complicated than rsync. rsync is even pretty much standard in many linuxes







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 18 '16 at 11:22

























            answered May 18 '16 at 9:27









            berserckberserck

            21626




            21626













            • are you saying this can't be easily done with cp? I'd rather use that since it's more clear to immediately realize what it's doing, but of course if it cannot be used in that way I can look for other options.

              – eis
              May 18 '16 at 10:02



















            • are you saying this can't be easily done with cp? I'd rather use that since it's more clear to immediately realize what it's doing, but of course if it cannot be used in that way I can look for other options.

              – eis
              May 18 '16 at 10:02

















            are you saying this can't be easily done with cp? I'd rather use that since it's more clear to immediately realize what it's doing, but of course if it cannot be used in that way I can look for other options.

            – eis
            May 18 '16 at 10:02





            are you saying this can't be easily done with cp? I'd rather use that since it's more clear to immediately realize what it's doing, but of course if it cannot be used in that way I can look for other options.

            – eis
            May 18 '16 at 10:02













            0














            cp indeed doesn't have an option to get only files, but as berserck said, you can use find together with cp. In fact, this is really straightforward:



            # Copy only files
            find /path/to/file -type f -execdir cp "{}" /dest/path ";"


            Or:



            # Copy everything except directories
            find /path/to/file -not -type d -execdir cp "{}" /dest/path ";"


            Explanation



            find is the tool that will look for the files you want. In the first option, we set -type f to return only regular files. In the second option, we set the -not -type d to get everything except directories. find executes the arguments passed to -execdir replacing {} with one result entry at a time and stops the command when ; is found (; should to be escaped ; or ";", or the shell might expand it).



            -execdir will run the command having the file's folder as its "working directory" and it is preferable instead of -exec, check below.



            The -type option documentation:




            -type c



            File is of type c:



            b: block (buffered) special



            c: character (unbuffered) special



            d: directory



            p: named pipe (FIFO)



            f: regular file



            l: symbolic link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken. If you want to search for symbolic links when -L is in effect, use -xtype.



            s: socket



            D: door (Solaris)




            The -not option documentation:




            ! expr



            True if expr is false. This character will also usually need protection from interpretation by the shell.



            -not expr



            Same as ! expr, but not POSIX compliant.




            The -execdir option documentation:




            -exec command ;



            Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of ';' is encountered. The string '{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command [...]. Both of these constructions might need to be escaped (with a '') or quoted to protect them from expansion by the shell. The specified command is run once for each matched file. The command is executed in the starting directory. There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.



            -execdir command ;



            Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find. This a much more secure method for invoking commands, as it avoids race conditions during resolution of the paths to the matched files. [...] If you use this option, you must ensure that your $PATH environment variable does not reference '.'; otherwise, an attacker can run any commands they like by leaving an appropriately-named file in a directory in which you will run -execdir. The same applies to having entries in $PATH which are empty or which are not absolute directory names.




            https://linux.die.net/man/1/find






            share|improve this answer




























              0














              cp indeed doesn't have an option to get only files, but as berserck said, you can use find together with cp. In fact, this is really straightforward:



              # Copy only files
              find /path/to/file -type f -execdir cp "{}" /dest/path ";"


              Or:



              # Copy everything except directories
              find /path/to/file -not -type d -execdir cp "{}" /dest/path ";"


              Explanation



              find is the tool that will look for the files you want. In the first option, we set -type f to return only regular files. In the second option, we set the -not -type d to get everything except directories. find executes the arguments passed to -execdir replacing {} with one result entry at a time and stops the command when ; is found (; should to be escaped ; or ";", or the shell might expand it).



              -execdir will run the command having the file's folder as its "working directory" and it is preferable instead of -exec, check below.



              The -type option documentation:




              -type c



              File is of type c:



              b: block (buffered) special



              c: character (unbuffered) special



              d: directory



              p: named pipe (FIFO)



              f: regular file



              l: symbolic link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken. If you want to search for symbolic links when -L is in effect, use -xtype.



              s: socket



              D: door (Solaris)




              The -not option documentation:




              ! expr



              True if expr is false. This character will also usually need protection from interpretation by the shell.



              -not expr



              Same as ! expr, but not POSIX compliant.




              The -execdir option documentation:




              -exec command ;



              Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of ';' is encountered. The string '{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command [...]. Both of these constructions might need to be escaped (with a '') or quoted to protect them from expansion by the shell. The specified command is run once for each matched file. The command is executed in the starting directory. There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.



              -execdir command ;



              Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find. This a much more secure method for invoking commands, as it avoids race conditions during resolution of the paths to the matched files. [...] If you use this option, you must ensure that your $PATH environment variable does not reference '.'; otherwise, an attacker can run any commands they like by leaving an appropriately-named file in a directory in which you will run -execdir. The same applies to having entries in $PATH which are empty or which are not absolute directory names.




              https://linux.die.net/man/1/find






              share|improve this answer


























                0












                0








                0







                cp indeed doesn't have an option to get only files, but as berserck said, you can use find together with cp. In fact, this is really straightforward:



                # Copy only files
                find /path/to/file -type f -execdir cp "{}" /dest/path ";"


                Or:



                # Copy everything except directories
                find /path/to/file -not -type d -execdir cp "{}" /dest/path ";"


                Explanation



                find is the tool that will look for the files you want. In the first option, we set -type f to return only regular files. In the second option, we set the -not -type d to get everything except directories. find executes the arguments passed to -execdir replacing {} with one result entry at a time and stops the command when ; is found (; should to be escaped ; or ";", or the shell might expand it).



                -execdir will run the command having the file's folder as its "working directory" and it is preferable instead of -exec, check below.



                The -type option documentation:




                -type c



                File is of type c:



                b: block (buffered) special



                c: character (unbuffered) special



                d: directory



                p: named pipe (FIFO)



                f: regular file



                l: symbolic link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken. If you want to search for symbolic links when -L is in effect, use -xtype.



                s: socket



                D: door (Solaris)




                The -not option documentation:




                ! expr



                True if expr is false. This character will also usually need protection from interpretation by the shell.



                -not expr



                Same as ! expr, but not POSIX compliant.




                The -execdir option documentation:




                -exec command ;



                Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of ';' is encountered. The string '{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command [...]. Both of these constructions might need to be escaped (with a '') or quoted to protect them from expansion by the shell. The specified command is run once for each matched file. The command is executed in the starting directory. There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.



                -execdir command ;



                Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find. This a much more secure method for invoking commands, as it avoids race conditions during resolution of the paths to the matched files. [...] If you use this option, you must ensure that your $PATH environment variable does not reference '.'; otherwise, an attacker can run any commands they like by leaving an appropriately-named file in a directory in which you will run -execdir. The same applies to having entries in $PATH which are empty or which are not absolute directory names.




                https://linux.die.net/man/1/find






                share|improve this answer













                cp indeed doesn't have an option to get only files, but as berserck said, you can use find together with cp. In fact, this is really straightforward:



                # Copy only files
                find /path/to/file -type f -execdir cp "{}" /dest/path ";"


                Or:



                # Copy everything except directories
                find /path/to/file -not -type d -execdir cp "{}" /dest/path ";"


                Explanation



                find is the tool that will look for the files you want. In the first option, we set -type f to return only regular files. In the second option, we set the -not -type d to get everything except directories. find executes the arguments passed to -execdir replacing {} with one result entry at a time and stops the command when ; is found (; should to be escaped ; or ";", or the shell might expand it).



                -execdir will run the command having the file's folder as its "working directory" and it is preferable instead of -exec, check below.



                The -type option documentation:




                -type c



                File is of type c:



                b: block (buffered) special



                c: character (unbuffered) special



                d: directory



                p: named pipe (FIFO)



                f: regular file



                l: symbolic link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken. If you want to search for symbolic links when -L is in effect, use -xtype.



                s: socket



                D: door (Solaris)




                The -not option documentation:




                ! expr



                True if expr is false. This character will also usually need protection from interpretation by the shell.



                -not expr



                Same as ! expr, but not POSIX compliant.




                The -execdir option documentation:




                -exec command ;



                Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of ';' is encountered. The string '{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command [...]. Both of these constructions might need to be escaped (with a '') or quoted to protect them from expansion by the shell. The specified command is run once for each matched file. The command is executed in the starting directory. There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.



                -execdir command ;



                Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find. This a much more secure method for invoking commands, as it avoids race conditions during resolution of the paths to the matched files. [...] If you use this option, you must ensure that your $PATH environment variable does not reference '.'; otherwise, an attacker can run any commands they like by leaving an appropriately-named file in a directory in which you will run -execdir. The same applies to having entries in $PATH which are empty or which are not absolute directory names.




                https://linux.die.net/man/1/find







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 14 at 2:47









                jpennajpenna

                1192




                1192






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Super User!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1078149%2fcp-only-files-skipping-directories%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

                    is 'sed' thread safe

                    How to make a Squid Proxy server?