Linux recursive copy files to its parent folder

Davita

I want to copy recursively files to its parent folder for a specific file extension. For example:

./folderA/folder1/*.txt to ./folderA/*.txt  
./folderB/folder2/*.txt to ./folderB/*.txt

etc.

I checked cp and find commands but couldn't get it working.

5gon12eder

I suspect that while you say copy, you actually mean to move the files up to their respective parent directories. It can be done easily using find:

$ find . -name '*.txt' -type f -execdir mv -n '{}' ../ \;

The above command recurses into the current directory . and then applies the following cascade of conditionals to each item found:

  1. -name '*.txt' will filter out only files that have the .txt extension
  2. -type f will filter out only regular files (eg, not directories that – for whatever reason – happen to have a name ending in .txt)
  3. -execdir mv -n '{}' ../ \; executes the command mv -n '{}' ../ in the containing directory where the {} is a placeholder for the matched file's name and the single quotes are needed to stop the shell from interpreting the curly braces. The ; terminates the command and again has to be escaped from the shell interpreting it.

I have passed the -n flag to the mv program to avoid accidentally overwriting an existing file.

The above command will transform the following file system tree

dir1/
    dir11/
        file3.txt
        file4.txt
    dir12/
    file2.txt
dir2/
    dir21/
        file6.dat
    dir22/
        dir221/
        dir221/file8.txt
        file7.txt
    file5.txt
dir3/
    file9.dat
file1.txt

into this one:

dir1/
    dir11/
    dir12/
    file3.txt
    file4.txt
dir2/
    dir21/
        file6.dat
    dir22/
        dir221/
        file8.txt
    file7.txt
dir3/
    file9.dat
file2.txt
file5.txt

To get rid of the empty directories, run

$ find . -type d -empty -delete

Again, this command will traverse the current directory . and then apply the following:

  1. -type d this time filters out only directories
  2. -empty filters out only those that are empty
  3. -delete deletes them.

Fine print: -execdir is not specified by POSIX, though major implementations (at least the GNU and BSD one) support it. If you need strict POSIX compliance, you'll have to make do with the less safe -exec which would need additional thought to be applied correctly in this case.

Finally, please try your commands in a test directory with dummy files, not your actual data. Especially with the -delete option of find, you can loose all your data quicker than you might imaging. Read the man page and, if that is not enough, the reference manual of find. Never blindly copy shell commands from random strangers posted on the internet if you don't understand them.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Recursive search, copy and rename of jpg files to parent folder name

From Dev

Copy Folders and its files with specific date in Linux

From Dev

Copy folder recursive in R

From Java

How do I copy folder with files to another folder in Unix/Linux?

From Dev

Copy files in same folder and replace year in its name

From Dev

How can I copy the contents of a child folder to its parent folder using command line?

From Dev

Linux command that Create and Copy Files and Folder in Multiple directories

From Dev

Copy top level folder structure without copying files in linux

From Dev

How to tell if folder is a subfolder for recursive folder copy?

From Dev

How do I move files and directories to the parent folder in Linux?

From Dev

XCOPY only files from a parent folder and from all its subforlders to a new folder in Windows?

From Dev

how to find and copy files in a sub directory from parent directory linux

From Dev

copy folder, subfolders and files from a path to another path in python via a recursive function

From Dev

Linux: Move files from unknown folder to unknown parent of the same unknown folder

From Dev

How do I copy a folder and its contents (files/subdirectories) in Python with platform independent implementation

From Dev

find files older than create its parent folder in a directory and move them there

From Dev

copy files from the parent location when I am inside a symbolically linked folder

From Dev

Copy only files with certain extension while renaming each file according to the parent folder

From Dev

"xargs -a file" to copy files to a folder

From Dev

copy files to other folder - find

From Dev

Copy specific files to another folder

From Dev

Command to copy only folder with files

From Dev

How to copy files to folder with execlp?

From Dev

How to copy files to a hidden folder

From Dev

copy files from folder to folder python

From Dev

copy files from folder to folder python

From Dev

Move files to parent folder in CMD

From Dev

Move files to parent folder in CMD

From Dev

Rename files to match parent folder

Related Related

  1. 1

    Recursive search, copy and rename of jpg files to parent folder name

  2. 2

    Copy Folders and its files with specific date in Linux

  3. 3

    Copy folder recursive in R

  4. 4

    How do I copy folder with files to another folder in Unix/Linux?

  5. 5

    Copy files in same folder and replace year in its name

  6. 6

    How can I copy the contents of a child folder to its parent folder using command line?

  7. 7

    Linux command that Create and Copy Files and Folder in Multiple directories

  8. 8

    Copy top level folder structure without copying files in linux

  9. 9

    How to tell if folder is a subfolder for recursive folder copy?

  10. 10

    How do I move files and directories to the parent folder in Linux?

  11. 11

    XCOPY only files from a parent folder and from all its subforlders to a new folder in Windows?

  12. 12

    how to find and copy files in a sub directory from parent directory linux

  13. 13

    copy folder, subfolders and files from a path to another path in python via a recursive function

  14. 14

    Linux: Move files from unknown folder to unknown parent of the same unknown folder

  15. 15

    How do I copy a folder and its contents (files/subdirectories) in Python with platform independent implementation

  16. 16

    find files older than create its parent folder in a directory and move them there

  17. 17

    copy files from the parent location when I am inside a symbolically linked folder

  18. 18

    Copy only files with certain extension while renaming each file according to the parent folder

  19. 19

    "xargs -a file" to copy files to a folder

  20. 20

    copy files to other folder - find

  21. 21

    Copy specific files to another folder

  22. 22

    Command to copy only folder with files

  23. 23

    How to copy files to folder with execlp?

  24. 24

    How to copy files to a hidden folder

  25. 25

    copy files from folder to folder python

  26. 26

    copy files from folder to folder python

  27. 27

    Move files to parent folder in CMD

  28. 28

    Move files to parent folder in CMD

  29. 29

    Rename files to match parent folder

HotTag

Archive