Find the answer to your Linux question:
Results 1 to 5 of 5
This must be a very basic question. say, ls -t |head -1 gets the last modified file in the directory. I would like to rename it using mv ls -t ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2012
    Posts
    5

    Question passing the result of pipe to next command


    This must be a very basic question.

    say,

    ls -t |head -1

    gets the last modified file in the directory.
    I would like to rename it using mv

    ls -t |head -1 |mv another.csv

    does not work obviously.
    I believe the pipe must pass the result to the argument of mv command.
    How can I do it?

    soichi

  2. #2
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,885
    You need to use the xargs command. I'm not certain if I've got this absolutely correct, but it's something like this:

    Code:
    $> ls -t | head -1 | xargs -0 -I file mv file another.csv
    Linux user #126863 - see http://linuxcounter.net/

  3. #3
    srj
    srj is offline
    Just Joined! srj's Avatar
    Join Date
    Jan 2012
    Location
    Bucharest
    Posts
    86
    Hi,

    You can try this:

    ls -t | head -1 > /tmp/tmp | mv `cat /tmp/tmp` another.csv

    Cheers,
    Srj

  4. #4
    Just Joined!
    Join Date
    Nov 2012
    Posts
    5
    Thank you all!

    soichi

  5. #5
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    i would have use command substitution:
    Code:
    mv $(ls -t|head -1) another.csv
    but xargs is probably the cleverer way to do it...

Posting Permissions

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