Find the answer to your Linux question:
Results 1 to 3 of 3
Hi All I have a file aa.txt I want to tar it. I'm using tar -cvf aa.tar aa.txt There's only one .txt file in my folder. I want to ls ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2007
    Posts
    10

    [SOLVED] Create file using tar and ls commands


    Hi All
    I have a file aa.txt

    I want to tar it. I'm using
    tar -cvf aa.tar aa.txt

    There's only one .txt file in my folder. I want to ls my folder, get the file's name and create a tar with its name

    ls *.txt
    aa.txt

    And I want not to give the tar file's name directly, but use the result of ls as tar file's name. It's name will be aa.txt.tar

    Thank you very much

  2. #2
    Linux Guru bigtomrodney's Avatar
    Join Date
    Nov 2004
    Location
    Ireland
    Posts
    6,133
    This is what springs to mind
    Code:
    tar -cvf "$(ls *.txt).tar" "$(ls *.txt)"
    Though as soon as you have more than one file that's going to fail. It should give you some ideas though; Using $() to execute a command within a command is a very effective way of doing things quickly and simply. Using $() is considered the more modern and more correct equivalent to backticks ``

  3. #3
    Just Joined!
    Join Date
    Jan 2007
    Posts
    10
    Wow you're great. This is what I want
    Thank you very much

  4. $spacer_open
    $spacer_close

Posting Permissions

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