find command

Hello,

I would like to rm all files begining with 't' without descending recursively. So if you have 2 files (say t1 and t2 under /tmp) and
(t3 and t4 under /tmp/test), ou like to rm only t1 and t2.

find /tmp/ -name 't*' -mtime +x .... ; ## to finish

Any help please

Thinks

Sof

Try:

find /tmp \( ! -name /tmp -prune \) -type f -name t\*....

I think a simple

rm */t*

should do it too. That way only files in the first level of directories will be deleted from.

sof,
You can try this.

find /tmp/ -name "t*" -exec rm -i {} \;

This will ask confirmation before deleting the file.
Hope this might help
Anent

I use the following to remove files with find:
<b>rm -f `find /tmp|grep t*`</b>
Note that the <b>`</b> is not the single quote located below the <b>"</b> but rather the angled quote.

You might want to use rm -i if you are unsure about what you might be deleting.

i think this is work also:

rm /tmp/t*

just as simple as this. :slight_smile:

rm */t* will not remove recursively will it? It will only remove files beginning with a t in all directories one level down (at least that is what happens on our systems here....)

Also,

rm -rf t* does not work because this will remove directories AND files that start with a t.

Also,

rm -f `find /tmp|grep t*` will also remove directories will it not?

So, I think you may have to add the -type f switch to find to insure that directories are not removed, right? Becomes:

rm -f `find -type f /tmp|grep t*

However, this also has problems because find returns the full path name so the syntax above will not work, seems to me :slight_smile:

Which implies that the suggestion

find /tmp/ -name "t*" -exec rm -i {} \;

Also does not work :slight_smile: because find returns the full path name on our systems. From here, seems like Perderabo's"

find /tmp \( ! -name /tmp -prune \) -type f -name t\*....

However, This does not work for me either !!!! :wink:

I suggest a variation of:

find /tmp -type f -regex .*\/t[^\/]*$ -exec rm {} \;

Which should work right?? :stuck_out_tongue: It finds only regular files, searches for t's after / and insures that no more /s exist before the end of the filename, then removes it......

BTW: Here is a URL to a summary for how to use regex (regular expressions)

Click
here for an even better page on Regular Expressions ....

To get a feel for what the expression can do try this:

find / -type f -regex .*\/t[^\/]*$ | more

This seems to work on our systems ......

<b>rm -f `find /tmp|grep t`</b> (note: you don't use * for grep'ing here)doesn't work exactly the way the question asked, but not for the reasons you brought up. The command will not remove directories unless you use the <b>-r</b> switch as well, and it certainly will remove files from <b>/tmp</b> as advertised. However, it will remove any file with a "t" in it. More correctly the command should be <b>rm -f `find /tmp|grep ^t`</b>. Note of course that anything that uses the find command will decend directories so anyting below the <b>/tmp</b> directory starting with a "t" will also be deleted. If you only want to delete in the <b>/tmp</b> directory then <b>rm -f /tmp/t*</b> is the correct procedure.:slight_smile:

Oppps! How about:
cd /
find tmp \( ! -name tmp -prune \) -type f -name t\*....

This time I tested it. I should know better than to post something like that without testing.

stuff like "rm t*" would seem to address the OP's original question until you notice the "-mtime" in his example.

Another concern is that "rm t*" will work only if there are a small number of files that start with t. Create 20,000 files that start with t in /tmp and now it will blow that max command line size on most versions of unix.

You need the path list as the first thing in a find command. So
rm -f `find /tmp -type f |grep t*`
will sort-of work. "grep t*" is going to match anything with zero or more t's in it, so that's not what we want. Also this will decend into subdirecties which is not what the op wanted.

This is nothing wrong with feeding full path names to rm except that it exacerbates the limited command line problem.

Once we have the find statement, rather than ending it with "-exec rm {} \;", I like "-print | xargs rm" which will fire up only as many rm processes as is needed rather than one per file. If someone does have 20,000 file that start with t, the difference is dramatic.

Yes, thanks for pointing that out. I misread the original poster and thought the question was to recursively go down.... however, on second read, the problem was easier (not to go recursively down).

My apologies for working 'the wrong' problem and any confusion it may have caused. BUT, thanks for the puzzle. I enjoyed working the regex for the wrong problem :cool: