Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion

Copy multiply files with rename
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Copy multiply files with rename

RazzaRazza Member
edited April 2017 in Help

Hi am trying to copy multiply file's but i want to rename the copied file to the name of the directory it was copy form.

The path to file are /home/{wildcard}/etc/{wildcard}/info

E.g /home/ryan/etc/test.tld/info would get copy as test.tld.

I had to type {wildcard} as * was not showing

Comments

  • ucxoucxo Member
    edited April 2017
    for x in /home/*/etc/*; do cp "$x/info" "${x##*/}"; done
    

    And * is not showing correctly because your forum post is parsed as Markdown, where *foo* makes foo appear in italics. ;)
    Escape it with a backslash to disable that behaviour: \*

    Thanked by 1ehab
  • ehabehab Member
    edited April 2017

    @ucxo said:

    i have a question for you would help me save time :)

    we can rename a file this way

    mv /some/path/mfile{,.old}

    the mfile will be renamed mfile.old

    now what what about rename it back to mfile ? in a geeky way

  • ucxoucxo Member

    @ehab said:

    @ucxo said:

    i have a question for you would help me save time :)

    we can rename a file this way

    mv /some/path/mfile{,.old}

    the mfile will be renamed mfile.old

    now what what about rename it back to mfile ?

    mv /some/path/mfile{.old,}? :)

    The string manipulation features don't really make your life easier here, since you would first need to assign the filename to a variable, e.g.:

    f="/some/path/mfile.old" mv "$f" "${f%.old}"
    

    The ${variable#substring}/${variable##substring}/${variable%substring}/${variable%%substring} notation (for removing matching substrings) and bash's other string manipulation features are explained here: http://tldp.org/LDP/abs/html/string-manipulation.html

    Thanked by 2Falzo ehab
  • ehabehab Member

    ucxo you are my hero :)

    @Flazo, i think we should get to know ucxo better hahah

    Thanked by 1Falzo
Sign In or Register to comment.