Find the answer to your Linux question:
Results 1 to 6 of 6
Hi how do you copy an entire line and add that copy at the end of the same line? For example, sometimes I rename a collection of files with a ...
  1. #1
    Just Joined!
    Join Date
    Feb 2011
    Posts
    2

    How to copy line to itself?

    Hi

    how do you copy an entire line and add that copy at the end of the same line?

    For example, sometimes I rename a collection of files with a command like:

    "mv oldfilename newfilename;"

    I am able to add "mv" at the beginning, the semicolon at the end and sometimes replace a word in the middle, but... how to change a line "oldfilename" into "oldfilename oldfilename"... that is already long time a mystery to me...

    Thanks a lot already in advance!

  2. #2
    Just Joined!
    Join Date
    Feb 2011
    Posts
    12
    You can store your string in a variable,

    Code:
    uname@ubuntu:~$ s="oldfilename"
    And then echo it twice,

    Code:
    uname@ubuntu:~$ echo $s $s
    Although I may have misunderstood your question. What exactly do you want to do?

  3. #3
    Linux Enthusiast Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    717
    in case you want to batch rename files, it could also be done with which uses perl expressions:

    man rename

  4. #4
    Just Joined!
    Join Date
    Dec 2010
    Posts
    16
    Code:
    [~]$ mkdir test
    [~]$ cd test
    [test]$ touch file1 file2 file3
    [test]$ ls -1d * | tee - | sort | xargs --max-args=2 | sed -e 's/^/mv -i /' -e 's/$/.old/'
    mv -i file1 file1.old
    mv -i file2 file2.old
    mv -i file3 file3.old
    That's for renaming.
    For moving the files, use:

    Code:
    [~]$ls -1d /home/me/test/* | tee - | sort | xargs --max-args=2 | sed -e 's/ \/.*\// \/abc\/def\/ghi\//' -e 's/^/mv -i /' 
    mv -i /home/me/test/file1 /abc/def/ghi/file1
    mv -i /home/me/test/file2 /abc/def/ghi/file2
    mv -i /home/me/test/file3 /abc/def/ghi/file3
    You have to use the absolute path, not a relative path.

  5. #5
    Just Joined!
    Join Date
    Feb 2011
    Posts
    2
    Already the idea of mnemo made me think: Why did I not think of that myself?

    But the ideas of Kloschüssel and Lahntaler deserve some more attention than I can give it now.

    But... thanks all! Here I found (possible) solutions to an old problem...

  6. #6
    Just Joined!
    Join Date
    Dec 2010
    Posts
    16
    If you use Ubuntu, then rename is a Perl script from the Perl Cookbook expecting different parameters than the rename used on other Linux distros. The rename used on the other distros is also available on Ubuntu with the program name rename.ul

Posting Permissions

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