Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, I've got a directory containing lots of .pc and .c files and would like to delete all the .c files where there is an associated .pc, but leave all ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2006
    Posts
    1

    Unhappy How to delete specific files?


    Hi,

    I've got a directory containing lots of .pc and .c files and would like to delete all the .c files where there is an associated .pc, but leave all other .c files alone, e.g. for the following files:

    file1.c file2.pc file2.c file3.pc file3.c file4.c file5.c

    I would like to run an rm command to just delete file2.c and file3.c

    I've seen this sort of thing in make files: rm -f $(@:.pc=.c) but this doesn't work in the shell (I'm using bash 2.05b)

    Can anyone help with this please?

    DJ

  2. #2
    Linux Guru techieMoe's Avatar
    Join Date
    Aug 2004
    Location
    Texas
    Posts
    9,496
    Code:
    rm file2.c file3.c
    Registered Linux user #270181
    TechieMoe's Tech Rants

  3. #3
    Just Joined! scottro's Avatar
    Join Date
    Feb 2006
    Posts
    57
    There's almost certainly a more elegant way to do this. However, if I understand your problem you have a bunch of files. Some are blah.c files but in other cases, you have blah.c and blah.pc. In those cases, you want to remove the blah.c file.

    The below should work
    Code:
    for i in $(ls | cut -d . -f 1 | uniq -d)
    rm ${i}.c
    done
    I would test this with echo first, e.g. echo rm ${i}.c which is always a good thing to do before you wind up deleting things you don't want to do. (The other option is rm -i which will ask you before removing each file--however, I like echo better, because I can see the result, and then just run it again without echo--if you use the -i option, you have to hit y for each file.

    If it's not clear in your browser, that's cut -d <space> and a period.

  4. #4
    Linux User muha's Avatar
    Join Date
    Jan 2006
    Posts
    290
    there could be a one-liner for it but i'd try it in a script and use something like:
    if [ -f .bash_profile ]
    then echo "you have bash_profile!"
    elif [ -f .bashrc ]
    echo "you have only bashrc. This could be an else without a condition"
    fi

    where you replace .bash_profile with the string for the file.

    -e file
    True if file exists.

    -f file
    True if file exists and is a regular file.

    more info: http://www.linuxcommand.org/wss0020.php#bashrc
    Now what? You have Linux installed and running. The GUI is working fine, but you are getting tired of changing your desktop themes. You keep seeing this "terminal" thing. Don't worry, they'll show you what to do @
    <~ http://www.linuxcommand.org/ ~>

Posting Permissions

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