Command to remove all files in a directory except .... - Unix

This is a discussion on Command to remove all files in a directory except .... - Unix ; Suppose I have the following files in my directory: a.dat, b.dat, c.dat, test.dat, test1.dat, test2.dat I want to be able to remove all the files in the directory except test.dat. Can someone kindly suggest the best way for realizing that. ...

+ Reply to Thread
Results 1 to 12 of 12

Thread: Command to remove all files in a directory except ....

  1. Command to remove all files in a directory except ....

    Suppose I have the following files in my directory:

    a.dat, b.dat, c.dat, test.dat, test1.dat, test2.dat


    I want to be able to remove all the files in the directory except
    test.dat. Can someone kindly suggest the best way for realizing that.

    -Choi


  2. Re: Command to remove all files in a directory except ....

    >Suppose I have the following files in my directory:

    >a.dat, b.dat, c.dat, test.dat, test1.dat, test2.dat


    >I want to be able to remove all the files in the directory except
    >test.dat. Can someone kindly suggest the best way for realizing that.


    mkdir .x
    mv * .x
    mv .x/test.dat .
    rm -rf .x

    Hubble.


  3. Re: Command to remove all files in a directory except ....

    usenet@sta.samsung.com writes:

    > I want to be able to remove all the files in the directory except
    > test.dat. Can someone kindly suggest the best way for realizing that.


    well, here's one simple way:

    chmod +w *.dat
    chmod -w test.dat
    echo no | /bin/rm *.dat

    --
    Sending unsolicited commercial e-mail to this account incurs a fee of
    $500 per message, and acknowledges the legality of this contract.

  4. Re: Command to remove all files in a directory except ....

    >Suppose I have the following files in my directory:

    >a.dat, b.dat, c.dat, test.dat, test1.dat, test2.dat


    >I want to be able to remove all the files in the directory except
    >test.dat. Can someone kindly suggest the best way for realizing that.


    Using backticks:


    rm `ls | grep -v '^test.dat$'`

    Hubble.


  5. Re: Command to remove all files in a directory except ....

    On 2005-12-14, Hubble wrote:
    >>Suppose I have the following files in my directory:

    >
    >>a.dat, b.dat, c.dat, test.dat, test1.dat, test2.dat

    >
    >>I want to be able to remove all the files in the directory except
    >>test.dat. Can someone kindly suggest the best way for realizing that.

    >
    > mkdir .x
    > mv * .x
    > mv .x/test.dat .
    > rm -rf .x
    >
    > Hubble.


    For bash
    shopt -s extglob
    rm !(test.dat)

    For ksh
    rm !(test.dat)

  6. Re: Command to remove all files in a directory except ....

    Bruce Barnett wrote, on Wed, 14 Dec 2005:

    > usenet@sta.samsung.com writes:
    >
    >> I want to be able to remove all the files in the directory except
    >> test.dat. Can someone kindly suggest the best way for realizing that.

    >
    > well, here's one simple way:
    >
    > chmod +w *.dat
    > chmod -w test.dat
    > echo no | /bin/rm *.dat


    No! That will remove all *.dat files (unless your rm is broken).

    If stdin is not a terminal, rm does not do interactive prompting
    for read-only files.

    --
    Geoff Clare


  7. Re: Command to remove all files in a directory except ....

    Geoff Clare writes:

    > Bruce Barnett wrote, on Wed, 14 Dec 2005:
    >> well, here's one simple way:
    >>
    >> chmod +w *.dat
    >> chmod -w test.dat
    >> echo no | /bin/rm *.dat

    >
    > No! That will remove all *.dat files (unless your rm is broken).
    >
    > If stdin is not a terminal, rm does not do interactive prompting
    > for read-only files.


    Thanks for the correction! I never realized until now that in
    /bin/rm -f *
    the -f is meaningless. Makes sense now that I think about it
    (backwards compatability and all). Heh. I've been putting it into
    cronjob scripts for years to make sure the files were deleted. (Smack
    Forehead).





    >
    > --
    > Geoff Clare
    >


    --
    Sending unsolicited commercial e-mail to this account incurs a fee of
    $500 per message, and acknowledges the legality of this contract.

  8. Re: Command to remove all files in a directory except ....

    ls -al *[!test.dat] shows a listing with everything but test.dat,
    simply subsitute rm for ls -al


  9. Re: Command to remove all files in a directory except ....

    "kd5foy" writes:

    >ls -al *[!test.dat] shows a listing with everything but test.dat,
    >simply subsitute rm for ls -al


    Maybe in the shell *you* use.....

    Mine does better with ls -d *[\!test.dat]

    --
    Chris.

  10. Re: Command to remove all files in a directory except ....

    Zawartość nagłówka ["Followup-To:" comp.unix.shell.]
    On 16.12.2005, Chris McDonald wrote:
    > "kd5foy" writes:
    >
    >>ls -al *[!test.dat] shows a listing with everything but test.dat,
    >>simply subsitute rm for ls -al

    >
    > Maybe in the shell *you* use.....
    >
    > Mine does better with ls -d *[\!test.dat]


    Oh really? Isn't it listing all files *ending* with anything except
    [test.dat], which is effectively equal to *[^adest.]? My all interactive
    shells (bash 3.0, zsh 4.2 and ksh93) are listing anything but files with
    such endings.

    NB, SUS defines only [!...] as negation of characters range. [^...] is
    an extension (quite common, however).

    --
    Feel free to correct my English
    Stanislaw Klekot

  11. Re: Command to remove all files in a directory except ....

    go throught this

    rm `ls | grep -v ""`


  12. Re: Command to remove all files in a directory except ....

    In article <1134574643.206144.302530@g49g2000cwa.googlegroups. com>,
    wrote:
    >Suppose I have the following files in my directory:
    >
    >a.dat, b.dat, c.dat, test.dat, test1.dat, test2.dat


    >I want to be able to remove all the files in the directory except
    >test.dat. Can someone kindly suggest the best way for realizing that.


    rm ?.dat test?.dat

    Bill

    --
    Bill Vermillion - bv @ wjv . com

+ Reply to Thread