1

I have a script for cleaning up of data files

The command:

for i in `find /ddomain/data/shop12/ -mtime +15 -print|grep -v "^.$" |grep data-`

I tried running the command without the grep -v function and I don't see any difference.

I would like to know what the grep -v "^.$" does. I did not create this script and I cannot find anything online.

3

grep -v "^.$" will print all lines from input except those that have only a single character.

As it is used in:

find /ddomain/data/shop12/ -mtime +15 -print|grep -v "^.$"

we can assume that it is to discard the current working directory (.) from find's output (shown when relative path . is used) , which can easily be achieved by -mindepth 1 option, although here it is redundant as you are using absolute path in find.

Later you are only searching for files with data- in their names by grep data-, this is redundant too. You can put all logic in a single find invocation:

find /ddomain/data/shop12/ -name '*data-*' -mtime +15 -print

Drop -print too, it is the default action:

find /ddomain/data/shop12/ -name '*data-*' -mtime +15

Just to note, if you are looking for only files, add -type f too:

find /ddomain/data/shop12/ -type f -name '*data-*' -mtime +15
|improve this answer|||||
  • And since anything found by find /ddomain/data/shop12/ will have the /ddomain/data/shop12/ prefix, there will be no such thing as a name formed of a single character. So the answer to the OP's question is: it does nothing at all. shrug – Satō Katsura Sep 26 '16 at 10:17
  • 1
    @SatoKatsura Yes, in this specific case. But the explicit question was: ”I would like to know what the grep -v "^.$" does” – maulinglawns Sep 26 '16 at 10:48
3

As others have already said, grep -v '^.$' filters out lines that consist of exactly one character (lines that consist of the beginning of the line (^) followed by one character (.) followed by the end of the line ($). Another way to write it would be grep -vx . where the -x makes the ^ and $ implicit (the . matches on the whole line).

On the output of:

find /ddomain/data/shop12/ -mtime +15 -print

That makes little sense because the output of that find command will typically consist of lines like:

/ddomain/data/shop12/foo
/ddomain/data/shop12/foo/bar
...

The only way for that output to contain lines with a single character is if there's a file whose name is something like foo<newline>X<newline>. That's perfectly possible, but I'd say it's unlikely it was the intent of that command.

Possibly, that command was (incorrectly) adapted from:

find . -mtime +15 -print | grep -v '^.$' | grep data-

Where that grep was intended to filter out the . entry (even then, grep -Fvx . would have been more appropriate). However, then it would have made more sense to write it:

find . ! -name . -path '*data-*' -mtime +15

If you want to filter-out the top-level directory with find /ddomain/data/shop12/, you'd do it as:

find /ddomain/data/shop12/. ! -name . -path '*data-*' -mtime +15

Or if your find implementation supports the -mindepth predicate:

find /ddomain/data/shop12/ -mindepth 1 -path '*data-*' -mtime +15
|improve this answer|||||
1

It removes the . directory from the list of find results. It's easier to explain by an example:

find . -print
.
./prova2
./prova2/prova2  

find . -print | grep -v '^.$'
./prova2
./prova2/prova2  

As you can see, in the first snippet of code, we find the . result. So, you pass it to the actions taken by the for cycle.
In the second snippet, you decide to skip the . directory from the results of find, so you don't send it to the for cycle.

|improve this answer|||||
  • 1
    Not only dot, but any one symbol result – Romeo Ninov Sep 26 '16 at 13:59
  • You're right. I've answered basing my explanation on the context of the script. I really think the meaning of that is cutting out the . folder. – Echoes_86 Sep 26 '16 at 14:06
0

The command grep -v "^.$" will "remove" from the stream the current directory and any file/directory which have name from one symbol. For example let execute

ls -a
.  ..  a1 a2 a3

and let exec it with the grep

ls -a |grep -v "^.$"
..
a1
a2
a3

As you can see the dot disappear

|improve this answer|||||
0

grep -v '^.$' excludes lines with only one character.

Example:

echo "foo
> bar
> x" | grep -v '^.$'
foo
bar
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.