0

I have multiple sub directories in which i have multiple csv file. I wanted to copy all the files alone into a different directory.

Am able to find or list the files using wildcards

find -name '*.csv' ls /**/'*.csv'

The challenge is all the directory names and file names are having spaces and so am unable to achieve the expected result.

Any suggestion will be helpful

Jim Macaulay
  • 4,709
  • 4
  • 28
  • 53
  • A correctly constructed command has no problem with spaces. We can't know what you did so we can't tell you where you went wrong. See https://mywiki.wooledge.org/BashFAQ/020 – tripleee May 12 '23 at 10:57
  • `find . -name '*.csv' -exec cp {} dest/ \;` will work portably, but is somewhat inefficient. `find . -name '*.csv' -exec cp -t dest {} +` is better, but requires GNU `cp`. Both of these will obviously overwrite files at the destination if you find several CSV files in different directories with the same name. – tripleee May 12 '23 at 10:59
  • @tripleee `find . -name '*.csv' -exec sh -c 'cp -- "$@" dest/' -- {} +` ? – jhnc May 12 '23 at 11:16
  • Yeah, that too; but again, we have a lot of existing duplicates with solutions. As they have not followed up with clarifications, I guess let's just close as a duplicate. – tripleee May 12 '23 at 11:50
  • Possible duplicate of https://stackoverflow.com/questions/2550791/how-can-i-copy-files-with-names-containing-spaces-and-unicode-when-using-a-shel – tripleee May 12 '23 at 11:51
  • Or perhaps simply [When to wrap quotes around a shell variable](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee May 12 '23 at 11:54
  • Does this answer your question? [How can I copy files with names containing spaces and UNICODE, when using a shell script?](https://stackoverflow.com/questions/2550791/how-can-i-copy-files-with-names-containing-spaces-and-unicode-when-using-a-shel) – bad_coder May 12 '23 at 15:23

1 Answers1

1
find . -type f -name "*.csv" -exec cp {} Folder \;

In that command, Folder is your different directory. And You have to create that directory before you run this command

Before you run this command

  • cd to your main directory or Home directory.
  • Create the directory that you want to copy all csv files (mkdir Folder)
PBS
  • 30
  • 5
  • If you think this question is an obvious duplicate of an existing question, please don't answer it. Once you earn enough reputation, you will be able to leave a comment to point out the duplicate target. – tripleee May 12 '23 at 11:02
  • The advice to `cd` somewhere is dubious. If they wanted to find files in their current working directory, the `find` command then needs to be adapted. A much better solution is to learn how to pass relative or absolute file names when required, and give up the superstition that `cd` is somehow necessary (though it can certainly be convenient). See also [What exactly is current working directory?](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory) – tripleee May 12 '23 at 11:09