Find the answer to your Linux question:
Results 1 to 5 of 5
Hi everyone I have 50 files in the following format. fileone.0001.txt filetwo.0002.txt filethree.0003.txt filefour.0004.txt filefive.0005.txt filesix.0006.txt fileseven.0007.txt up to filefifty.0050.txt Can someone please show me how to delete only the ...
  1. #1
    Linux User
    Join Date
    Jul 2007
    Location
    Greece
    Posts
    277

    how to delete a specific range of files

    Hi everyone

    I have 50 files in the following format.

    fileone.0001.txt
    filetwo.0002.txt
    filethree.0003.txt
    filefour.0004.txt
    filefive.0005.txt
    filesix.0006.txt
    fileseven.0007.txt up to filefifty.0050.txt


    Can someone please show me how to delete only the first 20 files.

    I can do
    Code:
    find . -type f -name '*txt' -exec rm -rf {} \;
    but this will delete everything.

    Can somebody please tell me how can I define the range of files I want to delete within this syntax?


    Many many thanks
    One Love!!!

  2. #2
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,097
    try this
    Code:
    for a in {1..20}; do b=`printf '%02d.txt' $a`; ls -1 *$b; done | xargs ls -la
    I am a bit uneasy with the *.
    It would be better to guard it more, perhaps if the files all start with the same name?
    Anyhow, if you feel comfortable, replace the ls with rm
    Last edited by Irithori; 08-13-2010 at 11:22 AM.
    You must always face the curtain with a bow.

  3. #3
    Linux User
    Join Date
    Jul 2007
    Location
    Greece
    Posts
    277
    Thanks a lot for your reply Irithori

    Yes the files all start with the same name, I shouldn't have rename them.

    They are all in the format
    file.0001.txt
    file.0002.txt
    and so on.

    When I run your script I got the errors below.

    Code:
    for: Command not found.
    a: Undefined variable.
    do: Command not found.
    b: Undefined variable.
    One Love!!!

  4. #4
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,097
    Which shell are you using?
    try
    Code:
    bash
    for a in {1..20}; do b=`printf 'file.00%02d.txt' $a`; ls -la $b; done
    And replace ls -la with rm if you are ok with the result
    You must always face the curtain with a bow.

  5. #5
    Linux User
    Join Date
    Jul 2007
    Location
    Greece
    Posts
    277
    Excellent!

    Thank you very much IRITHON

    See you in Octoberfest.
    One Love!!!

Posting Permissions

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