Find the answer to your Linux question:
Results 1 to 5 of 5
Hi All, I am planning to delete few files from a subdirectory, my aim is to locate all *.des files, remove them all except the one which the user specifies.here ...
  1. #1
    Just Joined!
    Join Date
    Mar 2009
    Posts
    8

    Post help needed in BASH scripting

    Hi All,

    I am planning to delete few files from a subdirectory, my aim is to locate all *.des files, remove them all except the one which the user specifies.here is the code i wrote which is not coming out right. des1 is already existing in the folder, and i am copying that to a new file with a different name and deleting all the *.des files in that folder.

    one more thing is, that there are also other type of files in the directory. so i just want to remove only the *.des files keeping the only one which the user specifies.

    #!/bin/bash
    echo "Enter Des File name"
    read des1
    echo " Enter the 5 digit name"
    read des2
    cp $des1 $des2
    touch $des2
    for i in `ls .`
    rm -f $i/*.des

    i know that above code removes all *.des files, wat command do i add to exclude that particular user given file.

    if you point me to the command name , i will figure out the code...

    Thanks

  2. #2
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    You can just use a simple if statement to check that i isn't equal to the given file.

    On another note you could also make your program cleaner and easier to understand by searching for .des files like so:

    Code:
    for i in `ls *.des`
    Which would mean you can delete the file by:

    Code:
    rm -f $i
    This will also make your if statement cleaner and easier to code. Good luck!
    Linux User #453176

  3. #3
    Just Joined!
    Join Date
    Mar 2009
    Posts
    8
    Thanks a lot..will do that

  4. #4
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    You could try something like this:

    Code:
    #!/bin/bash 
    
    read -p'Enter des file name to preserve: '
    
    shopt -s extglob
    
    rm --  */!("${REPLY%.des}").des
    Try the script with echo/printf instead of rm to verify the correct globbing.

  5. #5
    Just Joined!
    Join Date
    Mar 2009
    Posts
    8
    Thanks guys..i did try this one and it worked..

    find .not -name $des1 -iname "*.des" -exec rm -f {} \;

Posting Permissions

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