Results 1 to 9 of 9

Thread: copy a file to all folders and subfolders

  1. #1
    Join Date
    Oct 2008
    Location
    /usr/bin/
    Beans
    440
    Distro
    Ubuntu

    copy a file to all folders and subfolders

    How do I copy a file called readme.txt to all folders and sub folders in /home/test

    thanks

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,367
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: copy a file to all folders and subfolders

    try this
    Code:
    while read d; do cp readme.txt "$d"; done < <( find /home/test -type d )
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  3. #3
    Join Date
    Oct 2008
    Location
    /usr/bin/
    Beans
    440
    Distro
    Ubuntu

    Re: copy a file to all folders and subfolders

    Quote Originally Posted by Vaphell View Post
    try this
    Code:
    while read d; do cp readme.txt "$d"; done < <( find /home/test -type d )
    perfect, and how about to copy a symbolic link to all folders so if I ever need to change the text in the readme file it will change all

    thanks

  4. #4
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    4,265
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: copy a file to all folders and subfolders

    That's a bit complicated. I would go for a slightly simpler solution as follows.
    Code:
    find /home/test -xdev -type d -exec cp readme.txt {} ';'
    The -xdev prevents it from traversing other file systems, e.g. If you have mounted a partition somewhere within /home/test. Omit it if you do want it to traverse other file systems.

    Making a symbolic link would be similar.
    Code:
    find /home/test -xdev -type d -exec ln readme.txt {}/readme.txt ';'
    Warning: I am not at my computer right now so I cannot test this, but it should work.
    Problems with WINE?
    Full Circle Magazine :: Confused "allot"? :: Cheap Linux stickers
    In my day, we had outdoors in which to run, play, and socialise. Now we have computers to do those.

  5. #5
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,367
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: copy a file to all folders and subfolders

    so you need a single file and a bunch of symlinks? in that case replace cp with ln -s
    Code:
    ln -s "$PWD"/readme.txt "$d"
    i used that PWD because my tests indicated that the parameter is pasted verbatim into symlink (eg ln -s file /dir1/dir2 wont automagically create file -> ../../file in dir1/dir2 but broken file -> file)


    i don't really see how a loop is more complicated than find -exec. It doe the same thing, amount of code is similar and it's easier to debug and expand if the need arises.
    Last edited by Vaphell; October 16th, 2012 at 12:55 AM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  6. #6
    Join Date
    Oct 2008
    Location
    /usr/bin/
    Beans
    440
    Distro
    Ubuntu

    Re: copy a file to all folders and subfolders

    Quote Originally Posted by Paddy Landau View Post
    That's a bit complicated. I would go for a slightly simpler solution as follows.
    Code:
    find /home/test -xdev -type d -exec cp readme.txt {} ';'
    The -xdev prevents it from traversing other file systems, e.g. If you have mounted a partition somewhere within /home/test. Omit it if you do want it to traverse other file systems.

    Making a symbolic link would be similar.
    Code:
    find /home/test -xdev -type d -exec ln readme.txt {}/readme.txt ';'
    Warning: I am not at my computer right now so I cannot test this, but it should work.
    thanks I will give that a try

  7. #7
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    4,265
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: copy a file to all folders and subfolders

    Quote Originally Posted by Vaphell View Post
    i don't really see how a loop is more complicated than find -exec. It doe the same thing, amount of code is similar and it's easier to debug and expand if the need arises.
    You are right that there is not a huge difference. Using a single command is simpler to understand and therefore to debug, with fewer places to make mistakes — in that, I beg to differ from you.
    Problems with WINE?
    Full Circle Magazine :: Confused "allot"? :: Cheap Linux stickers
    In my day, we had outdoors in which to run, play, and socialise. Now we have computers to do those.

  8. #8
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,367
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: copy a file to all folders and subfolders

    while read something; do ... done < file | < <( some_command ) is as standard as it gets when it comes to traversing file or command output, after some thinking i agree that find is better in this case.
    Syntax of find has its idiosyncracies as well so its not all roses but with while apprach one has to be more careful about caveats like delimiters or characters that can break things, should they appear.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  9. #9
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    4,265
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: copy a file to all folders and subfolders

    Quote Originally Posted by Vaphell View Post
    Syntax of find has its idiosyncracies…
    LOL, syntax of every Linux command has its idiosyncrasies!
    Problems with WINE?
    Full Circle Magazine :: Confused "allot"? :: Cheap Linux stickers
    In my day, we had outdoors in which to run, play, and socialise. Now we have computers to do those.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •