开发者

find and grep command this but not that

开发者 https://www.devze.com 2023-04-01 06:04 出处:网络
I am trying to find all the files which are not the *.o (object) files and store in file MyFile.lst in the current directory Tree.

I am trying to find all the files which are not the *.o (object) files and store in file MyFile.lst in the current directory Tree.

I am doing it from using below command.

     #! /usr/bin/ksh
     find . -type f | grep -v "*.o" >> MyFile.lst

For some reason it is not working please help me.

Edit:

find . -type f | grep -v '\.o$' >> MyFile.lst

Seems like working. Any comment/ suggestion.(added keith.layne correc开发者_StackOverflow社区tion)


You don't need grep.

find . -type f  '!' -name '*.o' >> MyFile.lst

In grep, the searching pattern should be a regular expression. Therefore, ., $ and * are having special meaning.

.  means match any character
$  means match the end of a line
X* means match X, zero or more times(greedy)

grep -v '\.o$' would match files with .o extension. (You need to escape . for its literal meaning).


Your edit should be fine...$ (as I'm sure you know) should match the end of the line.

You should make one change, however: escape the . (a wildcard) with a \. Otherwise you'll match for example a file named 'Mo'.

I think (from a quick test) that grep treats * as a literal at the beginning of a pattern.

Your script will now be:

#! /usr/bin/ksh
find . -type f | grep -v '\.o$' >> MyFile.lst


find . -not -name "*.o" -type f >> list
0

精彩评论

暂无评论...
验证码 换一张
取 消