Ubuntu – Copy/Paste loop code

16.04bashcommand linedirectorypaste

I am new in Linux so here is my very basic question. I have a directory with 41 folders. I need to create a loop so that the process goes into each folder, copy all the files and then paste everything in a specific folder.

Any idea how to approach this? It would be a lot of clicking without a code solution

In the image you can see all the folders I need to access and the output folder where I want to paste everything (zPASTE_EVERYTHING_HERE)


My path is: /shared/home/data/output

Would like to paste everything in: /shared/home/data/output/zPASTE_EVERYTHING_HERE

Best Answer

  • Assuming that:

    • The folders to be copied and their files are under /home/gcgm/myfolders
    • The output folder is /tmp/output-folder

    If you need to copy all files to another folder, you can do the following:

    mkdir /tmp/output-folder
    cd /home/gcgm/myfolders
    cp */* /tmp/output-folder

    The meaning of */* in the this commandcp / target` is:

    • Take all files (the 2nd *) in all directories (the first *) under the current directory.
  • Related Question